httr POST authentication error - r

I am trying to structure a POST json request using httr. The API documentation proposes the following the CURL request:
curl -X POST -H "Authorization:Token XXXXXXXXX" -H "Content-Type: application/json" --data "{\"texts\":[\"A simple string\"]}" https://api.uclassify.com/v1/uclassify/topics/classify
My R httr implementation is the following:
POST("https://api.uclassify.com/v1/uClassify/Topics/classify",
encode="json",
add_headers('Authorization:Token'="XXXXXXXXX"),
body=("A simple string"))
But I received a 401 error message which indicates that my authentication failed. Any suggestion on how I can implement the CURL request on httr?

Just in case it can help somebody else, the below code works for me:
POST("https://api.uclassify.com/v1/uClassify/Topics/classify",
encode="json",
add_headers(Authorization = "Token XXXXXXXXX"),
body = "{\"texts\":[\"A simple string\"]}")

Related

HTTP POST using httr in R

I am trying to adapt my curl request from running it in Terminal to R. For this I am using the httr package. My request is as follows and works from Terminal
curl -u user#email.com:password -H "Content-Type:application/json" -X POST "https://catalogue.onda-dias.eu/dias-catalogue/Products(48809a01-71bc-4669-8639-f0528abcdafe)/Ens.Order"
My attempt to use httr POST function is as follows:
pars <- list(username='user#email.com', password='password')
res<-POST("https://catalogue.onda-dias.eu/dias-catalogue/Products(48809a01-71bc-4669-8639-f0528abcdafe)/Ens.Order", body = pars, add_headers('Content-Type'='application/json'))
I am getting a 401 error code. Do you know how should I properly convert my request?
Try using httr:authenticate
require(httr)
headers = c('Content-Type' = 'application/json')
r <- httr::POST( url = 'https://catalogue.onda-dias.eu/dias-catalogue/Products(48809a01-71bc-4669-8639-f0528abcdafe)/Ens.Order'
, httr::add_headers(.headers=headers)
, httr::authenticate('user#email.com', 'password'))

Adding comment to the pull request throws "Message: Not Found" error

I am trying to add commit to the pull request using GitHub API for issues. But seeing error
curl -H "Authorization: token "Key"" -X POST -d '{"body": "Failed"}' "https://github-site/repos/:repository/issues/PR_NUMBER/comments"
Response for the curl command.
{
"message": "Not Found",
"documentation_url": "https://developer.github.com/enterprise/2.15/v3/issues/comments/#create-a-comment"
}
Ah I found my mistake. I was passing basic authorizing keys. To Post the comment or to create a Label we need OAUTH token generated on github.
https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/

Use httr to create POST call for Bearer token

I am using R.
I am new to API's and trying to figure out how to put the post call together to get the required bearer token.
I am using the Experian Sandbox.
Once I have the bearer token i'm good but getting the Post call put together is proving to be very difficult for me.
+below was clipped from the developer portal.
The call to get the Oauth2 token is a POST request with a Content-Type
which needs to be specified as JSON; the response will also be in JSON
format:
Request example:
curl -X POST
-d '{"username": "youremail#email.com", "password": "YOURPASSWORD"}'
-H "Client_id: xxxxxxxxxxxxxxxxxxxxxxxx"
-H "Client_secret: xxxxxxxxx"
-H "Cache-Control: no-cache"
-H "Content-Type: application/json"
"https://sandbox-us-api.experian.com/oauth2/v1/token"
The following solution took care of my issue should anyone else need it for future reference. Thank you to R Community on helping to get me up to date on how this call is performed.
post_req <- httr::POST(
"https://sandbox-us-api.experian.com/oauth2/v1/token",
add_headers(
"Content-Type" = "application/json",
"Cache-Control"="no-cache",
"Client_secret"="xxxxxxxxxx",
"Client_id"="xxxxxxxxxxxxxxxxx"),
body = '{"username": "youremail#email.com", "password": "YOURPASSWORD"}',
verbose()
)

Using JSON in Header in Python Requests library

I want to make a request to an API which expects a JSON to be sent in the Header field. I'm unable to do it in Python Request library.
I'm able to do it in cURL.
cURL code:
curl -v -X POST https://content.dropboxapi.com/2/files/download
--header "Authorization: Bearer abcdefgh12343567"
--header "Dropbox-API-Arg: {\"path\": \"/folder/file.mp4\"}" -o file.mp4
Python code:
import requests
import simplejson
r = requests.post(
'https://content.dropboxapi.com/2/files/download',
headers={
'Authorization':'Bearer abcdefgh12343567',
'Dropbox-API-arg': simplejson.dumps({'path': '/folder/file.mp4'})
})
Here the Header contains a JSON string.
I'm trying to use Dropbox's files/download API documented here.
Even though the request is sent, the JSON value seems to be wrong.
The above code seems to work correctly now. I'm not sure about what happened - I was receiving HTTP 409 error earlier.
I was doubtful about how Python Requests Library would handle JSON string inside the header. So, I created an API to see how the server would read the headers made by both cURL and Python Requests. Both headers are identical.

Microsoft cognitive API token doesn't work

I'm trying to use the Microsoft cognitive API for text analysis using the recommended curl method from their documentation:
curl -v -X POST "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment" -H "Content-Type: application/jscp-Apim-Subscription-Key: {bc94cba9b84748ebb2f2b79a28ee3450}" --data-ascii "{I had a wonderful experience! The rooms were wonderful and the staff were helpful.}"
But I get back:
{ "statusCode": 401, "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription." }
I also tried removing the {} surrounding token and text to be analyzed. What am I doing wrong here?
Note: yes I realize the security issue with showing key but I have re-generated thanks.
There are three issues with your request:
Content-Type header should be application/json. This is likely a copy-paste error.
Ocp-Apim-Subscription-Key header value must be the API without the curly braces. This is the cause for your 401 error.
The body must be JSON of a particular format. You can find the schema here.
Here's the rewritten request:
curl -v "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/sentiment" -H "Content-Type: application/json" -H "Ocp-Apim-Subscription-Key: $OXFORD_TEXT_KEY" --data-ascii '{"documents":[{"language":"en","id":"1234","text":"I had a wonderful experience! The rooms were wonderful and the staff were helpful."}]}'
Which should result in:
{"documents":[{"score":0.9750894,"id":"1234"}],"errors":[]}

Resources