Thingsboard REST api always responds with status 401 - thingsboard

First I got a token using curl command as shown here. Then used this token to authorize swagger and tried some endpoints, but all of them responded with
{
"status": 401,
"message": "Authentication failed",
"errorCode": 10,
"timestamp": 1490619586352
}
On server side I get this exception:
2017-03-27 13:31:16,149 [http-nio-0.0.0.0-8080-exec-9] ERROR o.t.s.s.s.m.token.RawAccessJwtToken - Invalid JWT Token io.jsonwebtoken.MalformedJwtException: Unable to read JSON value: ��!L��ȉ
I also tried this with curl, with the same results, using this syntax:
curl -X GET --header 'Accept: application/json' --header 'Content-Type: application/json' --header 'X-Authorization: MY_TOKEN' 'http://MY_SERVER:MY_PORT/api/tenants?limit=3'
where I changed MY_TOKEN, MY_SERVER and MY_PORT appropriately for my server.

It seems that a parameter(Bearer) was missing from one of the headers. It should be --header 'X-Authorization: Bearer MY_TOKEN'. When I added it the responses were as expected. So the complete command for curl is:
curl -X GET --header 'Accept: application/json' --header 'Content-Type: application/json' --header 'X-Authorization: Bearer MY_TOKEN' 'http://MY_SERVER:MY_PORT/api/tenants?limit=3'

Related

Google Drive API - Invalid Credentials

I want to use a scripted approach (probably via) curl, to access some simple info from the drive api, like creation date. Essentially I want to script what I can do in their web interface: https://developers.google.com/drive/api/v3/reference/files/list.
I having been using a curl command that they expose in a query at the above link:
curl \
'https://www.googleapis.com/drive/v3/files?corpora=user&q=createdTime%20%3E%20%272021-11-23T12%3A00%3A00%27&key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
I have created an API key for this purpose (unrestricted for now). And used this app to generate an access token: https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=735795831119-kcpkamhiaojavqrt67mti7thcaa6ce87.apps.googleusercontent.com
But I have spent hours chasing my tail over the 401 Invalid Credentials error. Any help on getting a more specific error message, or better way to do this seemingly simple query would be appreciated. Thanks!
The result of the link below is an Authorization code.
https://accounts.google.com/o/oauth2/auth?scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=735795831119-kcpkamhiaojavqrt67mti7thcaa6ce87.apps.googleusercontent.com
You need to exchange it to https://accounts.google.com/o/oauth2/token to generate an Access Token:
curl \
--request POST \
--data "code=[Authentcation code from authorization link]&client_id=[Application Client Id]&client_secret=[Application Client Secret]&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code" \
https://accounts.google.com/o/oauth2/token
The result of the curl above is something like this:
{
"access_token": "access token here",
"expires_in": 3599,
"refresh_token": "refresh token here",
"scope": "https://www.googleapis.com/auth/drive",
"token_type": "Bearer"
}
Now you have the access token, you can paste it in the code below alongside with your API key.
curl \
'https://www.googleapis.com/drive/v3/files?corpora=user&q=createdTime%20%3E%20%272021-11-23T12%3A00%3A00%27&key=[YOUR_API_KEY]' \
--header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
--header 'Accept: application/json' \
--compressed
Note:
Make sure you enable the Drive API in GCP
Application Client Id and Application Client Secret can be found after you created an OAuth 2.0 Client ID in GCP.
Reference:
DaImTo answer on How to connect to the Google Drive API using cURL.

How to get token using Cloud Foundry api?

I'm trying to get token from Cloud Foundry. I'm getting token from cf oauth-token command but I try using curl its giving me an error:
{
"description": "Unknown request",
"error_code": "CF-NotFound",
"code": 10000
}
The Curl command I'm using:
curl 'https://<domian>/oauth/token' -X POST \
-H 'Content-Type: application/x-www-form-urlencoded' \
-H 'Accept: application/json
What am I doing wrong?
It depends on used flow. See doc: https://docs.cloudfoundry.org/api/uaa/version/4.31.0/index.html#authorization

Update Owner field of RT Ticket via REST API 2.0

curl -X PUT
-H "Content-Type: application/json"
-d
'{ “Owner”: { "id" : "root" } }'
-H 'Authorization: token XXX_Token_XXX'
'http://XXX_RT_URL_XXX/REST/2.0/ticket/6'
This command works for updating values of a ticket at the top level of JSON, but values such as 'Owner' fails. The documentation does not denote any method as to update these specific fields. What is the recommended way to update a ticket's Owner field via the Request Tracker REST API 2.0 (rt-extension-rest2)?
Try with the username as the value:
curl -X PUT
-H "Content-Type: application/json"
-d
'{ “Owner”: "root" }'
-H 'Authorization: token XXX_Token_XXX'
'http://XXX_RT_URL_XXX/REST/2.0/ticket/6'
That should accept a username or user ID.

Invalid token error in Linkedin Gem when its work in CURL of Rails app

When i used CURL post from console then its work but when i used linkedin gem method in controller then its not working and get error regarding access token. Where is wrong here not findout.
CURL Code
curl -v -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"comment":"hello from google www.google.com! http://ibm.com","visibility":{"code":"anyone"}}' https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=AQVxxxxJXygzp_8Exxxxg7_7FaxxxxxxtWzBXAxxxxxa5z1MVK6-kubHJ5JIaGAxxxx234wndpAMc_CxmCsIHxxxxraleZqkU0t_WNEhxxxz8_cKpeOixxxxsd15-X-MdvzYVxxxx9hQ&format=json&title=linkedin HTTP/1.1
LinkedIn gem code
client = LinkedIn::Client.new(
config[:your_consumer_key],
config[:your_consumer_secret]
)
client.authorize_from_access(
config[:oauth_user_token],
config[:oauth_user_secret]
)
client.add_share(
comment: 'Good Morning',
content: {'submitted-url' => 'http://www.github.com/blazeeboy' }
)
Any one have a idea where is wrong here.
============= UPDATE =============
system 'curl -H "Content-Type: application/json" -H "authToken: auth_token" --data '{"comment":"hello from google www.google.com! http://google.com","visibility":{"code":"anyone"}}' https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=AQVxxxxygzp_8E3ySvg7_7FahixxxxxxtWzBXAja5OQ8a0wta5z1MVK6-kubHJ5JIxxxxwndpAMc_CxmCsIHxFlexxxxxleZqkU0t_WNxxxxmi7CMz8_cKpexxxx15-X-MdvzYVOxxxxm9hQ&format=json&title=linkedin HTTP/1.1'
Thanks
curl -H "Content-Type: application/json" -H "authToken: auth_token" --data '{}' https://api.linkedin.com/v1/people/~/shares
Try running this :
system "curl -H 'Content-Type: application/json' -H 'authToken: auth_token' --data '{'comment':'hello from google www.google.com! http://google.com','visibility':{'code':'anyone'}}' https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=AQVxxxxygzp_8E3ySvg7_7FahixxxxxxtWzBXAja5OQ8a0wta5z1MVK6-kubHJ5JIxxxxwndpAMc_CxmCsIHxFlexxxxxleZqkU0t_WNxxxxmi7CMz8_cKpexxxx15-X-MdvzYVOxxxxm9hQ&format=json&title=linkedin HTTP/1.1"
In rails when you use double quotes inside single quotes it gives error with some commands.
OR you can try like this :
`curl -H 'Content-Type: application/json' -H 'authToken: auth_token' --data '{'comment':'hello from google www.google.com! http://google.com','visibility':{'code':'anyone'}}' https://api.linkedin.com/v1/people/~/shares?oauth2_access_token=AQVxxxxygzp_8E3ySvg7_7FahixxxxxxtWzBXAja5OQ8a0wta5z1MVK6-kubHJ5JIxxxxwndpAMc_CxmCsIHxFlexxxxxleZqkU0t_WNxxxxmi7CMz8_cKpexxxx15-X-MdvzYVOxxxxm9hQ&format=json&title=linkedin HTTP/1.1`

Weird json request via curl

I'm sending json request via curl to my local web server
curl -H 'Content-Type: application/json' -d {"name":"Stanford University", "subdomain":"stanford"} http://localhost:3000/rest/v1/groups.json
But in my rails app in params i'm getting
Parameters: {"name"=>"Stanford University", "subdomain"=>"stanford", "group"=>{"name"=>"Stanford University", "subdomain"=>"stanford"}}
Where did that hash group came from, and how do i controll it?
Look in config/initializers/wrap_parameters.rb. You likely have wrap_parameters enabled for JSON requests.
You can disable this option in the above file with
wrap_parameters false
Recommended Reading: http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html
You have to send it into Json Format
Try Following
curl -H 'Content-Type: application/json' -d "{\"name\":\"Stanford University\", \"subdomain\":\"stanford\"}"
OR
hash = {"name":"Stanford University", "subdomain":"stanford"}
curl -H 'Content-Type: application/json' -d "#{hash.to_json}" http://localhost:3000/rest/v1/groups.json

Resources