R to connect to github API but an credentials error - r

library(httr)
library(httpuv)
library(jsonlite)
myapi <- oauth_app("github", "0b23485d6d6e55143372",
secret = "xxxxxxxxxxxxxxxxxxxxxxx")
github_token <- oauth2.0_token(oauth_endpoints("github"), myapi)
request1 <- GET("https://api.github.com/users/jtleek/repos", config(token = github_token))
myjson <- content(request1)
myjson2 <- jsonlite::fromJSON(toJSON(myjson))
View(myjson2)
stop_for_status(request1)
#
I use R 3.2.5 and Win7 32 bit.
There are 2 problems. One is after the final line of code, I got an error Error: Unauthorized (HTTP 401).
And the second is when I tried to see what the github_token is,
<Token>
<credentials> error, error_description, error_uri
#
Finally myjson2 is not a dataframe but just a list
$message
[1] "Bad credentials"
$documentation_url
[1] "https://developer.github.com/v3"

I've had the same issue, so I have created a new set of key and secret on github page. By the way, I don't think you need to convert using jsonlite::, the format should be fine. I used the following:
gtoken <- config(token = github_token)
req <- with_config(gtoken, GET("https://api.github.com/users/jtleek/repos"))
con_req <- content(req)
find_create <- function(x,myurl) {
if (x$html_url == myurl) {
print(x$created_at)
}
}
lapply(con_req,find_create,myurl ="https://github.com/jtleek/datasharing")

Related

how can i fix the error check_twitter_oauth()

I am trying to use twitteR. I already have my keys and everything but I always get the following error:
Error in check_twitter_oauth() : OAuth authentication error:
This most likely means that you have incorrectly called setup_twitter_oauth()'
The code is the following:
library(twitteR)
# open json file
keys <- fromJSON(file = "path")
# keys
consumerKey <- keys$Key
consumerSecret <- keys$Secret
accessToken <- keys$Token
accessTokenSecret <- keys$TokenS
# connect to twitter
setup_twitter_oauth(consumerKey, consumerSecret, access_token=accessToken, access_secret=accessTokenSecret)
Why do I get the error?
Thanks

R AzureGraph | How to get callRecords from Graph

With the premise that it's the first time I'm working with Graph API... I can't seem to get a handle on how to retrieve callRecords with the AzureGraph package. I'm using an existing app registration.
I wrote my code based on the vignette of the package (which uses examples for delegated authentication), however I'm not sure how to proceed...
This is what works:
library(AzureAuth)
library(AzureGraph)
library(tidyverse)
library(dplyr)
library(rjson)
library(jsonlite)
library(RCurl)
# Create graph login
gr <- create_graph_login()
# My info
me <- gr$get_user()
# App Authentication
AppID <- "xAppID"
TenantID <- "xTenantId"
Secret <- "xSecret"
App <- gr$get_app(AppID)
service <- App$get_service_principal()
# Graph API Information
Version <- getOption("azure_graph_api_version")
Endpoint <- "https://graph.microsoft.com"
Tok <- AzureAuth::get_azure_token(Endpoint, tenant = TenantID, app = AppID, password = Secret)
And this is where I get stuck:
firstpage <-call_graph_endpoint(Tok, "me/memberOf")
pager <- ms_graph_pager$new(Tok, firstpage)
pager$has_data()
pager$value
isFALSE(pager$has_data())
is.null(pager$value)
This gives me an error:
> firstpage <-call_graph_endpoint(Tok, "me/memberOf")
Error in process_response(res, match.arg(http_status_handler), simplify) :
Bad Request (HTTP 400). Failed to complete operation. Message:
/me request is only valid with delegated authentication flow.
> pager <- ms_graph_pager$new(Tok, firstpage)
> pager$has_data()
[1] FALSE
> pager$value
list()
> isFALSE(pager$has_data())
[1] TRUE
> is.null(pager$value)
[1] FALSE
I then tried to change the operation parameter, but I cannot understand what I'm supposed to fetch. This is one random attempt:
firstpage <-call_graph_endpoint(Tok, "$metadata#calls")
pager <- ms_graph_pager$new(Tok, firstpage)
pager$has_data()
pager$value
isFALSE(pager$has_data())
is.null(pager$value)
This doesn't give me any errors, but I don't know what to do with it:
Second way:
firstdf <- call_graph_endpoint(Tok, operation = "$metadata#calls", options = list('$top'=1), simplify=TRUE)
pager <- ms_graph_pager$new(Tok, firstdf)
df <- NULL
while (pager$has_data()) {
df <- vctrs::vec_rbin(df, pager$value)
This throws me a bad request error.
So... my first problem is that I don't know what my operation string should look like.
Thanks in advance for throwing some light on this...

Create own Google Translate API call

I try to call the Google Translate API but always get a 403 error that I didn't provide a valid API key.
I did create one, though, on my Google Cloud Platforms website here: https://console.cloud.google.com/apis/credentials
Here's what I'm doing:
library(jsonlite)
library(httr)
my_text <- c("Hallo Mama", "Ja älskar dig")
my_key <- "MYKEY"
textbody <- toJSON(list(q = my_text,
target = "en"))
translated <- POST(url = paste0("https://translation.googleapis.com/language/translate/v2"),
add_headers("key" = my_key),
content_type("application/json"),
body = textbody,
encode = "json")
But this returns said 403 error, i.e.:
fromJSON(rawToChar(translated$content))
gives:
$error
$error$code
[1] 403
$error$message
[1] "The request is missing a valid API key."
$error$errors
message domain reason
1 The request is missing a valid API key. global forbidden
$error$status
[1] "PERMISSION_DENIED"
Note: the "MYKEY" used above I have of course replaced with my created true API key.
Any ideas what's going on?
Update: the following does work for v2 of the API (but not v3). I was missing te unboxing in the JSON body and I seem to have to put the APIkey into the URL:
textbody <- toJSON(list(q = my_text,
target = "en"),
auto_unbox = TRUE)
translated <- POST(url = paste0("https://translation.googleapis.com/language/translate/v2?key=", my_key),
content_type("application/json"),
body = textbody,
encode = "json")

Updating Google Tag Manager container via API using R: invalidArgument error

I am trying to write an R script to programmatically update a Google Tag Manager container via API and I have hit a bit of a wall getting it to work, as it keeps returning an invalid argument error. The problem is that I can't quite figure out what the problem is.
The documentation for the API call is here:
https://developers.google.com/tag-manager/api/v2/reference/accounts/containers/update
Here's the code:
library(httr)
url_base <- 'https://www.googleapis.com/tagmanager/v2'
url_path <- paste('accounts',account_id,'containers',container_id,sep='/')
api_url <- paste(url_base,url_path,sep='/')
#since the instructions indicate that the request body parameters are all optional, let's just send a new name
call <- PUT(api_url,
add_headers(Authorization = paste("Bearer", gtm_token$credentials$access_token)),
encode = 'json',
body = list(name = 'new name'))
call_content <- content(call,'parsed')
This is a pretty standard API call to the GTM API, and in fact I have written a bunch of functions for other GTM API methods that work in the same way, so I am a bit perplexed as to why this one keeps failing:
$error
$error$errors
$error$errors[[1]]
$error$errors[[1]]$domain
[1] "global"
$error$errors[[1]]$reason
[1] "invalidArgument"
$error$errors[[1]]$message
[1] "Bad Request"
$error$code
[1] 400
$error$message
[1] "Bad Request"
It seems like the issue is in the message body, but it's not clear if the issue is down to the API expecting different information / more parameters, when the documentation suggests that all of the parameters are optional.
OK, so the documentation is lacking here. This works if you include a name at least. Here's a working function:
gtm_containers_update <- function(account_id,container_id,container_name,usage_context,domain_name,notes,token) {
require(httr)
token$refresh()
#create the post url
api_url <- paste('https://www.googleapis.com/tagmanager/v2','accounts',account_id,'containers',container_id,sep='/')
#create the list with required components
call_body <- list(name = container_name,
usageContext = list(usage_context),
notes = notes,
domainName = domain_name)
call <- POST(url,
add_headers(Authorization = paste("Bearer", token$credentials$access_token)),
encode = 'json',
body = call_body)
print(paste('Status code:',call$status_code))
}

POST update to Twitter using Oauth and HTTR, R

I am trying to post an update to Twitter using the api. Following the steps at https://github.com/hadley/httr/blob/master/demo/oauth1-twitter.r, I can get the GET call to work, but I haven't managed to get the POST to work. Here is the relevant code:
library(httr)
library(httpuv)
oauth_endpoints("twitter")
myapp <- oauth_app("twitter",
key = "key"
secret = "secret"
)
twitter_token <- oauth1.0_token(oauth_endpoints("twitter"), myapp)
#this works fine
req <- GET("https://api.twitter.com/1.1/statuses/home_timeline.json",
config(token = twitter_token))
#this doesn't work. Returns error 403: status is forbidden
POST(url = "https://api.twitter.com/1.1/statuses/update.json",
body = "test",
config(token = twitter_token)
)
Any solution that allows a status update (Tweet) from R is acceptable.
The docs say you need to pass a parameter status as a query parameter, not in the body, which makes sense b/c it can only be 140 characters long anyway.
POST("https://api.twitter.com/1.1/statuses/update.json",
query = list(status = "testing from R"),
config(token = twitter_token)
)

Resources