I have to post some JSON to a restful endpoint and decided to do this in R. I am aware of the httr library. However, I am not sure how to add an access token for Azure active directory. This is some code:
library(httr)
access_token <- "SomeSecretStuffeyJ0eXAiOiJKV"
json <- '{"test_data":"hello_world"}'
uri <- "https://somewhere.net/bla"
result <- POST(
uri
, content_type_json()
, body = json
)
I am not sure if I can do this using httr? Could someone please help out with code and/or suggest alternative library? Thanks!
Related
I want to log in https://adventofcode.com/2021 programatically using httr.
I have only a very superficial understanding of the OAuth "dance", so to understand the principles I did the following:
Create an OAuth App on GitHub
Used the following code to authenticate (N.B. I know I could simply use oauth2.0_token but I felt that this workflow helped me to better understand the different messages, which are exchanged).
library(httr)
client_id <- "<my client id>"
base_url <- "https://github.com"
path <- "login/oauth/authorize"
client_secret <- "<my secret>"
url <- modify_url(base_url, path = path,
query = list(client_id = client_id,
redirect_uri = oauth_callback()))
code <- oauth_listener(url) ## needed to provide credentials in the web browser
access_token <- POST(modify_url(base_url, path = "login/oauth/access_token"),
add_headers(Accept = "application/json"),
body = list(client_id = client_id,
client_secret = client_secret,
code = code$code))
## successfully returns the values
GET("https://api.github.com/rate_limit",
add_headers(Authorization = paste(content(access_token)$access_token,
"OAUTH-TOKEN")))
From this example I think I understand the steps as highlighted in the documentation.
However, I fail to see how I could use this to login to https://adventofcode.com/2021. I have, of course, not the client_secret nor can I redirect the response to my localhost (as GitHub requires that the stored callback matches the redirect URI).
Thus, I was wondering how I could programatically login to https://adventofcode.com/2021 to fetch my task data, say?
I think you are mismatching OAuth2 roles. If you want to use adventofcode.com, you are a resource owner, adventofcode.com is a client and github is an authorization server. So you authenticate, adventofcode.com gets an auth code and then tokens. They can use the tokens to get information about your github account.
The example code you posted is different - your application is a client that gets a code and tokens from the authorization server (github) after a user was authenticated and gave a consent to passing the tokens to your app (permission delegation). So you probably cannot include adventofcode.com into this scenario.
The only way is if adventofcode.com takes a role of a resource server and their API accepts github tokens from different clients. But I know nothing about their API.
I am trying to download data in R via an API using OData protocol. I am new to APIs so please bear with me.
I am trying to send a GET command using the following from the API specs:
https://address/v1/meter?$filter=GroupGUID eq guid’ID’
where address is replaced by the URL and ID is replaced by the ID of the meter.
The API uses basic authorization, so I am using the following code from the httr library:
MyUrl <- "https://address/v1/meter?$filter=GroupGUID eq guid'ID'"
MyData <- GET(MyUrl, authenticate("Username", "access code", "basic"))
This gives an status code 400 - The request is badly formed
The authentication works, when I am not using the $filter command.
I have been in touch with the developers of the API and they have confirmed that my GET command is correct. But they are not familiar with R, so they cannot help. I suspect that it may be the space before and after “eq” that is causing the problem.
Can anyone help or point me to a description of using the Odata protocol in R?
Best regards, Rune
I finally figured it out. If I replace space " " with "%20", it works.
This solution is also described her: passing odata $filter in httr GET request
I'm trying to generate an access token for my Flickr app in httr. My goal is to download images from certain geographical co-ordinates. To my understanding this isn't something the FlickrAPI package can do, so I've decided to use httr.
Running the below code brings up the authorization page but when I submit the authorization I get a localhost error (localhost unexpectedly closed the connection).
What am I forgetting here? I haven't set a callback URL, if that's relevant.
library(httr)
setwd("G:\\Project\\R Code\\")
api_key <- "<TOKEN>"
secret <- "<SECRET>"
flickr.app <- oauth_app("App Name", api_key, secret)
flickr.endpoint <- oauth_endpoint(
request = "https://www.flickr.com/services/oauth/request_token",
authorize = "https://www.flickr.com/services/oauth/authorize",
access = "https://www.flickr.com/services/oauth/access_token"
)
token <- oauth1.0_token(
flickr.endpoint,
flickr.app,
cache = FALSE
)
I am looking to use Bing API to extract data in R. When I am using the following query in my browser
https://api.datamarket.azure.com/Bing/Search/Web?Query=%27analytics%27&$format=json
I am getting the search results in json format. But I have to enter the UserID (Blank) and password (authorization token) for authorization first.
How can I merge the query with the authorization token in R so that I can directly load the search results in my R environment?
You can use authenticate function as a part of httr package:
library(httr)
response = GET(url = 'https://api.datamarket.azure.com/Bing/Search/Web?Query=%27analytics%27&$format=json',
authenticate('','auth token', type = 'basic'))
res = content(response, encoding = 'json')
I have a Client ID and Client Secret after having set up an application in github, I'm not sure what the URL or the callback URL is meant to be for that...which i think is causing me problems
I also have a private repo that I would like the application to access...
The way I would like to access the private repo would be via R, so I have found some packages that might help including ROAuth and oauth, but I'm not too sure how to go about using these to get the tokens, as they tend to require a bunch of URLs to make the requests from, and I am unsure as to what the URLs are to get these requests for tokens.
Looking at http://developer.github.com/v3/oauth/ doesn't seem to be amazingly helpful in terms of my inputs for oauth or Oauth2Authorize functions for each of the respective packages.
The end goal is to source files from the private repo, since source_url('private.repo.file.url') doesn't work
I tried the basic authentication using curl through bash, but wasn't able to find a token.
Any walkthrough examples of how to go about doing this would be greatly appreciated.
P.S. this is a follow up question from r sourcing private repos from github
You just need to create an oAuth token at https://github.com/settings/tokens
and get required file via GitHub API using code like below
library(RCurl)
library(devtools)
jsonRawFile <- fromJSON(getURL("https://api.github.com/repos/USERNAME/REPONAME/contents/filename.R",
httpheader = c(Authorization = "token 38ebb0393fe1757ffde9c45d81adzzzzzzzzz",
"User-Agent" = "RCurl"),
.opts = list(ssl.verifypeer = FALSE)))
source_url(jsonRawFile$download_url)
The format of Authorization header should be strictly "token " + your_token_from_account.