Converting cURL command to JavaScript - node.js

I'm trying to call an Instagram API call from my server (nodeJS) and I am not sure how to do so.
curl -X POST https://api.instagram.com/oauth/access_token \
-F client_id=123456... \
-F client_secret=123abc... \
-F grant_type=authorization_code \
-F redirect_uri=https://google.sg/ \
-F code=123abc
This command returns the desired output when I run it on the command line, but I can't seem to find a way to do the same in JavaScript. Here is what I've tried:
axios
.post("https://api.instagram.com/oauth/access_token", {
client_id: 123456...,
client_secret: "123abc...",
grant_type: "authorization_code",
redirect_uri: "https://google.sg",
code:
"123abc...",
})
This is what is logged in by the catch block:
data: {
error_type: 'OAuthException',
code: 400,
error_message: 'Missing required field client_id'
}
I think -F refers to form data, but I can't seem to find a way to do that in axios.
edit: I've tried it with just client_id, and it returns the same error.
edit 2: I don't think this is a fix but Postman Agent has a nifty function to translate the HTTP request into code:
It can be accessed via Code on the right.

Just see that in your CURL request you have the redirect_uri: https://google.sg/
And in your axios params you have: https://google.sg
You are missing a / in the axios parameter.
The way Oauth2 works is that it appends parameters to the redirect_uri specified in your api settings, so if you set it to https://google.sg/, it is expected that it is indeed the same url, without any changes.
The response you should be getting would be:
https://google.sg/access_token=#xxxx
As you had specified, the response would be different, like so:
https://google.sg?access_token=#xxxx
That is not only a non-valid URL, but it also doesn't match the one you specified.
For this reason it is recommended that you set a standard path, for example:
https://google.sg/oauth
So in the end you would get:
https://google.sg/oauth?access_token=#xxxx

Related

Spotify API authorization works with cURL but not with python requests

I am trying to get an acess token from the Spotify API. This is the curl command for it :
curl -H "Authorization: Basic M2...xMTJiNDc=" -d "grant_type=authorization_code" -d "code=AQAXvqFp....29kd09" -d "redirect_uri=https%3A%2F%2Fexample.com%2Fcallback" https://accounts.spotify.com/api/token
This works.
However,if I try to run it using python requests,I get a "invalid authorization" token error. I have reproduced the steps to get the auth token multiple times,but still same error.
I read on their GitHub that the auth code works only once,so I used the new auth code with python requests first,but still no luck. This is the python code:
import requests as req
data = {
'grant_type': 'authorization_code',
'code':'AQAXvqFp........9kd09',
'state': '34fFs29kd09=34fFs29kd09',
'redirect_uri': 'https://example.com/callback'
}
header={
"Authorization":f"Basic {authbasic}" #same as in the curl command
}
url = "https://accounts.spotify.com/api/token"
response = req.post(url,headers=header,data=data)
print(response.content)
This is the error:
b'{"error":"invalid_grant","error_description":"Invalid authorization code"}'
How do I resolve this? I am using Python3.6

why calling curl from execSync in Node.js fails but directly run the exact-same command works?

I have come into a trouble that when using execSync in node.js, it's not working as directly type the command in the shell.
Here is my issue:
I use a curl to request for some data from a server, and I need to do that with a cookie because there is a login requirement.
It's easy to handle the login process and get the cookie, but it's weird that using the cookie with a curl in node.js would cause the server an "internal error". And since I don't have the permission to change the server-code, I'm looking for help about the difference of calling curl in Node.js and directly use curl.
Here is the code:
var command = 'curl --cookie cookie.txt ' + getURL();
console.log(command);
// output: curl --cookie cookie.txt http://example.com/getdata
var result = child_process.execSync(command).toString();
// will cause an internal error and the "result" is an error-reporting page.
Directly calling this in the shell:
curl --cookie cookie.txt http://example.com/getdata
Everything works, I got the data I need.
I tried to find some plots, for instance, changing the code to:
var command = 'curl --cookie cookie-bad.txt ' + getURL();
I put some wrong cookie in the cookie-bad.txt, I will get a "you are not log in" result.
So there must be something wrong with:
sending a cookie to the server to request some data with curl running inside a nodejs script with execSync.
Is there any way I can improve the code or something?
What is your Node.js version? I don't have any problem with 10.16.0.

How to fetch user using the TrueVault API

I'm trying to fetch a user from the TrueVault API using curl.
I've been using the following curl command (as per the documentation):
curl https://api.truevault.com/v2/users/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx7ce \
-X GET \
-u API_KEY:"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx461"
The credentials:
user_id: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx7ce
api key: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx461
Every time I send the request I'm getting an error:
"error": {
"code": "AUTH.UNSUCCESSFUL",
"message": "Authentication Failure.",
"type": "invalid_request_error"
},
"result": "error",
"transaction_id": "xxxxxxxx-xxxx-xxxx-xxxx-3ba883e31f99"
So I wanted to check if maybe I'm doing wrong or might be something else.
This user has all permissions (Create, Read, Update, Delete).
It looks like your Auth Header is malformed. You can read more about how to build the Auth Header here: https://docs.truevault.com/overview#authentication.
For a simple curl you can use the -u option instead of building the base64 Basic Auth header. -u expects username:password, but TrueVault's API is token-based so they want you to do "$API_KEY:". Note the empty second component! That trailing : is critical.
Try this:
curl https://api.truevault.com/v2/users/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx7ce \
-X GET \
-u "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxx461:"

Access Public URLs from IBM Cloud storage

I have created a IBM cloud Object storage service and i have created objects i.e image files in it.
I am looking for the steps to access the images as public URLs. I did some initial research and found there are cURL commands using swift Client to do this.
Reference links
How to access files in container in Object Storage Service in Bluemix? AND
Public URLs For Objects In Bluemix Object Storage Service
from the above links, it says the following steps
Set up the swift CLI . Can you link me the steps to set up Swift CLI ? ( the link in the reference link dosen't work anymore ).
2.Change the container ACL to read with following PUT request
curl -X PUT "https://dal.objectstorage.open.softlayer.com/v1/AUTH_123/mycontainer" \
-H "X-Auth-Token: token123" \
-H "X-Container-Read: .r:*"
But i am not sure what to input on X-Auth-Token header ? i have the following information from the service credentials of COS.
{
"apikey": "X7aDm6yu123123hXwqvq1231232HgOtIGeZiAOEg",
"endpoints": "https://cos-service.bluemix.net/endpoints",
"iam_apikey_description": "Auto generated apikey during resource-key operation for Instance - crn:v1:bluemix:public:cloud-object-storage:global:a/f9aabca54c702be8386b2a3f9815b4e4:d145a33e-e8b1-446f-a87d-69431eaec0b1::",
"iam_apikey_name": "auto-generated-apikey-bed16ed5-1373-47bc-b268-5e0f521bc802",
"iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Writer",
"iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::a/f9aabca54c702be8386b2a3f9815b4e4::serviceid:ServiceId-36c373a0-4bb9-4316-bc4b-86ea4c98dcd7",
"resource_instance_id": "crn:v1:bluemix:public:cloud-object-storage:global:a/f9aabca54c702be8386b2a3f9815b4e4:d145a33e-e8b1-446f-a87d-69431eaec0b1::"
}
Any help would be really appreciated. Thanks
The bearer token is from IAM.
https://console.bluemix.net/docs/services/cloud-object-storage/getting-started-cli.html#gather-key-information
See this doc on generating pre-signed urls (temporary).
https://console.bluemix.net/docs/services/cloud-object-storage/api-reference/api-reference-objects.html#object-operations
To do a one-off request like that, you can get oauth tokens from the command line using ibmcloud iam oauth-tokens. To specifically get an IAM Token, I use:
export IAM_TOKEN=`ibmcloud iam oauth-tokens | head -n 1 | awk ' {print $4} '`
Then follow that up with your cURL command:
curl -H "Authorization: Bearer $IAM_TOKEN" ...
An application should request tokens based on the apiKey as mentioned.
curl -X POST -H "Content-Type: application/x-www-form-urlencoded" -H "Cache-Control: no-cache" -d 'apikey=<your api key here>&grant_type=urn:ibm:params:oauth:grant-type:apikey' "https://iam.bluemix.net/identity/token"
Here's an example using NPM's request-promise:
const response = await rp({
url: 'https://iam.bluemix.net/identity/token',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: "POST",
body: `apikey=${apiKey}&grant_type=urn%3Aibm%3Aparams%3Aoauth%3Agrant-type%3Aapikey`,
json: true
});
const token = response.access_token;

Github API Create Issues return 404 Not found

I am making a request to the below URL-
Post https://api.github.com/repos/kvimal/2048/issues
With my Token as a header for authorization.
The Curl Request
curl -i -X POST https://api.github.com/repos/kvimal/2048/issues -d "{title:'hey'}" -H "Authorization: Bearer xxxxxxxxxxxxxxxxxx" -H "Content-Type: application/json"
And GitHub sends a response 404 Not found. I have reade the Documentation and as far as i have observed i am doing it by the github standards. Can anyone Help with this issues?
As illustrated in this python script, the header should be using 'token' not Bearer'
headers = {
'Content-Type':'application/json',
'Authorization': 'token %s' % token,
}
(That script doesn't use curl, but give an idea of the header)
For curl queries, see this curl POST tutorial:
curl -H "Authorization: token OAUTH-TOKEN"
And the POST message must be complete as well (as in this python script)
issue = {'title': title,
'body': body,
'assignee': assignee,
'milestone': milestone,
'labels': labels}
# Add the issue to our repository
r = session.post(url, json=issue)
(again, not curl, but gives you an example of the body)
Go to "Edit personal access token" page, checkout the "Select scopes" session.
Make sure the token has been granted required access right.
I encountered similar case when trying to create public repo with 'Authorization: token ...' header.

Resources