The following error occurs when creating a device using python rest api in things board CE.
Is it impossible to create a device using the python rest api?
error
Note: For device_profile_id, the value returned by text search using python rest api was used.
I confirmed that the device profile id matches the device profile id copied from the web admin page.
HTTP response headers: HTTPHeaderDict({'Vary': 'Origin, Access-Control-Request-Method, Access-Control-Request-Headers', 'X-Content-Type-Options': 'nosniff', 'X-XSS- Protection': '1; mode=block', 'Cache-Control': 'no-cache, no-store, max-age=0, must-revalidate', 'Pragma': 'no-cache', 'Expires' : '0', 'Content-Type': 'application/json;charset=ISO-8859-1', 'Content-Length': '336', 'Date': 'Fri, 07 Oct 2022 01:10:20 GMT', 'Via': '1.1 google'})
HTTP response body: b'{"status":500,"message":"JSON parse error: (was java.lang.ClassCastException); nested exception is com.fasterxml.jackson.databind.JsonMappingException: (was java.lang. ClassCastException) (through reference chain: org.thingsboard.server.common.data.Device[\\"deviceProfileId\\"])","errorCode":2,"timestamp":"2022-10-07T01:10:20.504 +00:00"}'
Related
I have see many article about this issue, but it all can not solve my problem.
I use python to insert data which code is the offical offer.
from datetime import datetime
from influxdb_client import InfluxDBClient, Point, WritePrecision
from influxdb_client.client.write_api import SYNCHRONOUS
# You can generate an API token from the "API Tokens Tab" in the UI
token = "myToken"
org = "myOrg"
bucket = "Test"
with InfluxDBClient(url="http://127.0.0.1:8086", token=token, org=org) as client:
but it seem st wrong with my influxdb.conf?
influxdb_client.rest.ApiException: (401)
Reason: Unauthorized
HTTP response headers: HTTPHeaderDict({'Content-Type': 'application/json; charset=utf-8', 'X-Influxdb-Build': 'OSS', 'X-Influxdb-Version': '2.1.1', 'X-Platform-Error-Code': 'unauthorized', 'Date': 'Mon, 07 Mar 2022 09:20:36 GMT', 'Content-Length': '55'})
HTTP response body: {"code":"unauthorized","message":"unauthorized access"}
I have an API Gateway that publishes the request in SQS. This Gateway has SQS SendMessage policy, and this issue I am describing here does not happen when I use the Test option of the gateway.
I am using Insomnia to make requests to the server, with these headers:
Content-Type: application/x-www-form-urlencoded
x-api-key: <My API Key>
Basically the configuration was done following this tutorial:
https://medium.com/#pranaysankpal/aws-api-gateway-proxy-for-sqs-simple-queue-service-5b08fe18ce50
The only difference I included the MessageGroupId on the Mapping template for deduplication:
Action=SendMessage&MessageBody=$input.body&MessageGroupId=$context.resourcePath
The response I am getting is vey weird, because it is a Http 200, with header:
date: Mon, 12 Apr 2021 13:05:53 GMT
content-type: application/json
content-length: 377
x-amzn-requestid: xxxxxxxxxxxxxxxxxxx
x-amz-apigw-id: xxxxxxxxxxxxx
x-amzn-trace-id: Root=xxxxxx
And the body:
<?xml version="1.0"?>
<ErrorResponse
xmlns="http://queue.amazonaws.com/doc/2009-02-01/">
<Error>
<Type>Sender</Type>
<Code>AccessDenied</Code>
<Message>Access to the resource https://sqs.xxxxxxx.amazonaws.com/arn:aws:sqs:us-xxxx-x:xxxxxxxx:xxxxxxxxx.fifo is denied.</Message>
<Detail/>
</Error>
<RequestId>xxxxxxxxxxxxxx</RequestId>
</ErrorResponse>
x's are used to hide information I am not sure are sensible.
I am not sure how to proceed, it seems to an issue while publishing in the queue.
In my inexperience, I was not aware that API Gateway requires Deploy for most of the changes.
I have a RoR application from where I need to call a 3rd party API with several different calls. GET requests work fine with both Postman and HTTParty, but I can't get POST to work with latter. In Postman, I have a default request with basic auth, headers and a half dozen parameters in the body with the default form-data option.
The Postman request works fine, but I've been having hard time to replicate the request to HTTParty, leading to the API responding with error as parameters are missing.
Here's some of the code I've been working with to no avail:
options = { headers: {"Accept"=>"application/json", "Content-Type"=>"application/json"},
:debug_output => $stdout, body: { "param1" => 1, "param2" => 5,
"param3" => 'foo', "param4" => 'bar', "param5" => 'test' }
.to_json,
basic_auth: {:username=>'censored', :password=>'censored'} }
response = HTTParty.post("#{Settings.api_url}", options)
opening connection to ...
opened
starting SSL for ...
SSL established
<- "POST ... HTTP/1.1\r\nAccept: application/json\r\n
Content-Type: application/json\r\nAuthorization: Basic censored\r\nConnection: close\r\n
Host: www.censored.com\r\n
Content-Length: 113\r\n\r\n"
<- "{\"param1\":1,\"param2\":5,\"param3\":\"foo\",\"param4\":\"bar\",\"param5\":\"test\"}"
-> "HTTP/1.1 200 OK\r\n"
-> "Server: Apache\r\n"
-> "Set-Cookie: PHPSESSID=...; path=/\r\n"
-> "Expires: Thu, 19 Nov 1981 08:52:00 GMT\r\n"
-> "Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0\r\n"
-> "Pragma: no-cache\r\n"
-> "Content-Length: 61\r\n"
-> "Connection: close\r\n"
-> "Content-Type: application/json\r\n"
-> "\r\n"
reading 61 bytes...
and then comes the error response. I tried with various combinations regarding body, also trying to set Accept and Content-Type headers to multipart/formdata but didn't get it working. How could I replicate the Postman request?
I'm trying to follow the documentation "https://developers.google.com/accounts/docs/OAuth_ref" to migrate oAuth to oAuth2 but keep getting an error
In the "APIs & auth" - "Credentials" Section in our API developers console we have 1 Client ID for web application set up along with a number of service account client Ids.
The client Ids appear to be in a format xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com for each client ID that is set up.
If I use the exact Id for the 'client ID for web application' in the format [xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com] then I get an error
{
"error" : "invalid_client"
}
If I use the more generic client ID [ xxxxxxxxxxxx.apps.googleusercontent.com
] then I get the following error
{
"error" : "disabled_client",
"error_description" : "The OAuth client was disabled."
}
Here is my post request from Fiddler
POST https://accounts.google.com/o/oauth2/token HTTP/1.1
Authorization: OAuth realm="",oauth_consumer_key="<consumerKey>",oauth_token="<token>",oauth_timestamp="1400680750",oauth_nonce="6637551",oauth_signature_method="HMAC-SHA1",oauth_signature="I%2FCOsR1BrGQHnqTeyhX4GUrKrv8%3D"
Content-Type: application/x-www-form-urlencoded
Host: accounts.google.com
Content-Length: 151
Expect: 100-continue
Connection: Keep-Alive
grant_type=urn:ietf:params:oauth:grant-type:migration:oauth1&client_id=<clientID>.apps.googleusercontent.com&client_secret={<client_secret>}
Here is the base string I use for oauth_signature
POST&https://accounts.google.com/o/oauth2/token&client_id=<clientID>.apps.googleusercontent.com&client_secret=<clientSecret>&grant_type=urn:ietf:params:oauth:grant-type:migration:oauth1&oauth_consumer_key=<consumerKey>&oauth_nonce=2648138&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1400681371&oauth_token=<token>
Here is the response I get from Google
HTTP/1.1 401 Unauthorized
Content-Type: application/json; charset=utf-8
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Pragma: no-cache
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Date: Wed, 21 May 2014 13:59:16 GMT
Content-Disposition: attachment; filename="json.txt"; filename*=UTF-8''json.txt
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Server: GSE
Alternate-Protocol: 443:quic
Transfer-Encoding: chunked
5b
{
"error" : "disabled_client",
"error_description" : "The OAuth client was disabled."
}
0
Any suggestions?
Here a related post: https://groups.google.com/forum/#!topic/google-analytics-data-export-api/yveoPwSVzCQ
As for Owen's suggestion, I am pretty sure the error is not related to oauth1 vs oauth2 client type validation but rather to the provided oauth2 credentials (client id and client secret).
It turns out that the POST body that I was sending to google was incorrect.
Originally I had sent
grant_type=urn:ietf:params:oauth:grant-type:migration:oauth1&client_id=<clientID>.apps.googleusercontent.com&client_secret={<client_secret>}
Note the { } around the client_secret. When I removed these then I no longer got the errors.
Now I can pass in the client_id in the format xxxxxxxxxxxx-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com and the client_secret without { } and I get a sucessful response.
The reason for the other error that I had been receiving was that the client id in the format xxxxxxxxxxxx.apps.googleusercontent.com was an old client_id that had been deleted and was no longer visible on the Google Developer console.
I am trying to access accounts.google.com to get token from authorization code received using HTTP post request.
var searchurl = "https://accounts.google.com/o/oauth2/token";
$.ajax({
dataType: "json",
url:searchurl,
data: {code:auth_code, client_id:'client_id', client_secret:'secret', redirect_uri:'http%3A%2F%2Flocalhost:8085%2FGmailIntegration%2FgetAuthResponse1.jsp', grant_type:'authorization_code'},
type:"Post",
contentType:"application/x-www-form-urlencoded",
success:function(data) {
alert(data);
},
error: function(jqXHR, exception) {
console.log(jqXHR);
}
});
Error:
"NetworkError: 400 Bad Request - https://accounts.google.com/o/oauth2/token?
code=4/PlKII3f0vsPUhl1QNIUXkiIhlfGA.sq9lFf-oCiIcXE-sT2ZLcbRFnpEphQI&client_id={clientid}
&client_secret={secret}&redirect_uri=https://oauth2-login-
demo.appspot.com/code&grant_type=authorization_code"
Request:
Response Headers
Alternate-Protocol 443:quic
Cache-Control no-cache, no-store, max-age=0, must-revalidate
Content-Encoding gzip
Content-Type application/json
Date Tue, 26 Nov 2013 14:20:56 GMT
Expires Fri, 01 Jan 1990 00:00:00 GMT
Pragma no-cache
Server GSE
X-Firefox-Spdy 3
X-Frame-Options SAMEORIGIN
X-XSS-Protection 1; mode=block
x-content-type-options nosniff
Request Header:
Accept application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Cache-Control no-cache
Connection keep-alive
Content-Length 0
Content-Type application/x-www-form-urlencoded
Host accounts.google.com
Origin http://localhost:8085
Pragma no-cache
here is the document which i am using:
After the web server receives the authorization code, it may exchange the authorization code for an access token and a refresh token. This request is an HTTPs post, and includes the following parameters:
Field Description
code The authorization code returned from the initial request
client_id The client_id obtained during application registration
client_secret The client secret obtained during application registration
redirect_uri The URI registered with the application
grant_type As defined in the OAuth 2.0 specification, this field must contain a value of authorization_code
The actual request might look like:
POST /o/oauth2/token HTTP/1.1
Host: accounts.google.com
Content-Type: application/x-www-form-urlencoded
code=4/P7q7W91a-oMsCeLvIaQm6bTrgtp7&
client_id=8819981768.apps.googleusercontent.com&
client_secret={client_secret}&
redirect_uri=https://oauth2-login-demo.appspot.com/code&
grant_type=authorization_code
A successful response to this request contains the following fields:
Field Description
access_token The token that can be sent to a Google API
refresh_token A token that may be used to obtain a new access token. Refresh tokens are valid until the user revokes access. This field is only present if access_type=offline is included in the authorization code request.
expires_in The remaining lifetime on the access token
token_type Indicates the type of token returned. At this time, this field will always have the value Bearer
I got this working.. i am sharing the code for those who are stuck with this:
$.ajax({
dataType: "json",
url:searchurl,
data: {code:code, client_id:'clientid', client_secret:'secret', redirect_uri:'http://localhost:8085/GmailIntegration/getAuthResponse.jsp', grant_type:'authorization_code'},
type:"POST",
contentType:"application/x-www-form-urlencoded; charset=utf-8",
crossDomain:true,
cache : true,
success:function(data) {
alert(data);
},
error: function(jqXHR, exception, errorstr) {
console.log(jqXHR);
alert(errorstr);
}
});
but now i have new issue. The url get 200 OK response but i am not getting response at all