Authentication OAuth2 to API REST using cURL - shell

i'm having a little problem here. I've been trying to authenticate (protocol OAuth2) to a REST API using the cURL command. I use the following command line :
curl -X POST -d "client_id=login_id&client_secret=password&grant_type=client_credentials" http://localhost:9001/rest/oauth/token
But it doesn't work and the command returns :
{"status":"error","message":"Parameter is required for method Load."}
But I don't know parameter they are talking about. Do you have any idea where the problem could come from ?

Related

Http POST requests on Heroku running Springboot app

I've successfully deployed my Springboot app on Heroku. I'm also able to make get request like :
curl GET 'heroku-url'
Correctly returns :
{"id":1,"user_id":1,"title":"rneptdqxen","image":"","price":0.0,"description":"Niente"}
but not POST, if I try this :
curl -X POST 'heroku-url -d '{}' '
An error is thrown :
empty string within braces in URL position 49
If I change heroku-url to 127.0.0.1:8080/..., build Springboot app locally then POST request works so I think that the request is correct, probably I'm missing something about quotes.
please try
curl -X POST -d '{}' 'heroku-url'

ecobee API thermostat request (json) using bash and curl

I'm writing a bash script to interface with my ecobee (query info and change settings). I have the authorization all worked out (access token and refresh token) and am now trying to request info from the ecobee. This json parameter list is dynamically created. None of the curl examples in the Developers API Doc seem to work.
I've tried assigning the json to a variable (?json="$selection") and to a file (?json=#"$location"). My latest attempt (hard coding the json and escaping the braces) of the curl is as follows:
response=$(curl -s "https://api.ecobee.com/1/thermostat?json=\{"selection":\{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true/}/}" -H "Content-Type: application/json;charset=UTF-8" -H "Authorization: Bearer $access_token")
and I received a null response declare -- response=""
If I have curl read from a file:
{"selection":{"selectionType":"registered","selectionMatch":null,"includeRuntime":true,"includeSettings":true}}
then I receive:
response='{
"status": {
"code": 4,
"message": "Serialization error. Malformed json. Check your request and parameters are valid."
}
}'
Which I assuming it' an escape issue?
Can anyone offer any insight? Again, I thought this would be the easy part. I'm using jq to build my request json. Other alternatives to curl that can better deal with json? I'm limited to bash (which is what I know)
Thanks
To integrate (arbitrary) data into a URL (or URI in general) you need to prepare it using percent-encoding first.
As you have mentioned to use jq to compose that data, you could add #uri to your jq filter to perform the encoding, and use the --raw-output (or -r) option to then output it properly.
For example, jq -r '#uri' applied to your JSON
{
"selection": {
"selectionType": "registered",
"selectionMatch": null,
"includeRuntime": true,
"includeSettings": true
}
}
would output
%7B%22selection%22%3A%7B%22selectionType%22%3A%22registered%22%2C%22selectionMatch%22%3Anull%2C%22includeRuntime%22%3Atrue%2C%22includeSettings%22%3Atrue%7D%7D
which you can use within the query string ?json=….

Obtaining Authorization Code from Spring OAuth 2.0 Authorization Server programmatically

I am trying to obtain an authorization code from a Spring OAuth 2.0 authorization server using simple CURL command.
curl -v --header "Authorization: Basic hasfhfashfakhsfakhf712641246" "http://0.0.0.0:0000/oauth-server/oauth2/authorize?response_type=code&client_id=dummyclient&client_secret=dummyclient&redirect_uri=http://oauth2server/oauth2callback/"
However, instead of getting back a redirect URL with the code, I am getting back a 302 redirect response to the login URL. Since I am already sending the username, password in the Authorization Header, is there a way to skip the login page redirect and get the Authorization Code directly?
you might need to tell curl to follow redirect with the -L flag
curl -L -v --header "Authorization: Basic hasfhfashfakhsfakhf712641246" "http://0.0.0.0:0000/oauth-server/oauth2/authorize?response_type=code&client_id=dummyclient&client_secret=dummyclient&redirect_uri=http://oauth2server/oauth2callback/"

Cannot get imgur v3 api request to authorize using curl or other bash utility

I'm trying to scrape some public data from imgur.com using the v3 imgur api but whenever I make a request using curl I'm getting a 401.
This is how I'm calling the API (using this example from the example python app) -
curl -F "Authorization=Client-ID <my_client_id_provided_by_registering_my_app>" https://api.imgur.com/3/gallery/hot/viral/0.json
I've also tried
curl -F "client_id=<my_client_id>" https://api.imgur.com/3/gallery/hot/viral/0.json
So my question is, what format is the imgur api expecting for the auth header and how can I leverage it using curl or some other common bash utility?
The switch -F (or --form) is for sending HTTP forms. What you should use instead is -H (--header):
curl --header "Authorization: Client-ID $YOUR_ID" https://api.imgur.com/3/gallery/hot/viral/0.json

Google App Scripts curl authorization

Just trying to play with google app scripts. In anonymous mode things seem fine. Except that anyone can call my script simply like that snippet shows:
curl "https://script.google.com/macros/s/.../exec?ip=\"$myIp\""
I used this manual for tips on how to authenticate through GoogleLogin. The problem is "401 Unauthorized" I received when sent auth token and "Me(owner)/Only myself" options were set on google side. (The token seems correct itself. If I omit password or mistype it, then I receive "Bad auth") If I set "Anyone, even anonymous" again, it works, but auth stuff seems like ignored. What's the correct way to do the trick?
#!/bin/bash
gmail=$1
password=$2
myIp=$3
GoogleAuthToken=""
GoogleAuthToken=`curl --silent https://www.google.com/accounts/ClientLogin --data-urlencode Email=$gmail \
--data-urlencode Passwd=$password -d accountType=GOOGLE -d source=YouDontSay -d service=lh2`
echo $GoogleAuthToken
GoogleAuthToken=$(echo "$GoogleAuthToken" | grep 'Auth=' | sed s/Auth=//)
echo $GoogleAuthToken
curl -L --silent --header "Authorization: GoogleLogin auth=$GoogleAuthToken" "https://script.google.com/macros/s/.../exec?ip=\"$myIp\""
You use ClientLogin
https://www.google.com/accounts/ClientLogin
This is google error :
Important: ClientLogin has been officially deprecated since April 20,
2012 and is now no longer available. Requests to ClientLogin will fail
with a HTTP 404 response. We encourage you to migrate to OAuth 2.0 as
soon as possible.

Resources