Influxdb Unauthorized 401 with python insert data - influxdb

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"}

Related

things board python rest api device registration error

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"}'

Request returns 401 Unauthorized with headers using HTTParty

i can get response from API using curl like this:
curl -X GET https://myapi.com/... -H "client_id: my_id" -H "client_secret: my_secret"
but when i send requests with HTTParty:
response = HTTParty.get(url, headers: {"client_id" => "my_id", "client_secret" => "my_secret"})
the result is:
#<HTTParty::Response:0x55e5db898160 parsed_response="Unable to retrieve client_id from message",
#response=#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>, #headers={"cache-control"=>["no-cache"], "pragma"=>["no-cache"], "content-length"=>["41"], "content-type"=>["text/plain"], "expires"=>["-1"], "server"=>["Microsoft-IIS/8.5"], "strict-transport-security"=>["max-age=31536000; includeSubdomains;"], "x-aspnet-version"=>["4.0.30319"], "x-powered-by"=>["ASP.NET"], "access-control-allow-origin"=>["*"], "access-control-allow-credentials"=>["true"], "access-control-allow-headers"=>["*"], "access-control-allow-methods"=>["GET, POST, PUT, DELETE, OPTIONS"], "date"=>["Wed, 13 Apr 2022 08:56:56 GMT"], "connection"=>["close"]}>
and output from response.request is:
#<HTTParty::Request:0x000055e5db897120 #changed_hosts=false, #credentials_sent=false, #http_method=Net::HTTP::Get,
#options={:limit=>5, :assume_utf16_is_big_endian=>true, :default_params=>{}, :follow_redirects=>true, :parser=>HTTParty::Parser, :uri_adapter=>URI, :connection_adapter=>HTTParty::ConnectionAdapter, :headers=>{"client_id"=>"my_id", "client_secret"=>"my_secret"}},
#path=#<URI::HTTP http://my_api.com/...>, #last_uri=#<URI::HTTP http://my_api.com/...>, #raw_request=#<Net::HTTP::Get GET>, #last_response=#<Net::HTTPUnauthorized 401 Unauthorized readbody=true>>
my opinion is, the headers goes in #options parameter instead of #headers, so request can't find my client_id. Does anyone know how can i fix this?
Do not use eval on an API response. Its extremely insecure. Use JSON.parse.

OAuth token submitted with the request can not be parsed

I'm trying to implement a client that imports the events that a user has in Office 365 so that I can easily display them in the company's application.
I managed to get the user to authenticate with his / her Office 365 account and to approve my application and to also get an AccessToken, but when I try to use the token to retrieve the events from the API, I get a 401 HTTP error code, no body and in the headers I have this:
Content-Length →0
Date →Thu, 17 Mar 2016 08:56:00 GMT
Server →Microsoft-IIS/8.0
WWW-Authenticate →Bearer client_id="00000002-0000-0ff1-ce00-000000000000", trusted_issuers="00000001-0000-0000-c000-000000000000#*", token_types="app_asserted_user_v1 service_asserted_app_v1", authorization_uri="https://login.windows.net/common/oauth2/authorize", error="invalid_token",Basic Realm="",Basic Realm=""
X-BEServer →DB4PR06MB522
X-BackEndHttpStatus →401
X-CalculatedBETarget →DB4PR06MB522.eurprd06.prod.outlook.com
X-DiagInfo →DB4PR06MB522
X-FEServer →AM3PR06CA022
X-Powered-By →ASP.NET
request-id →de1963bc-36df-4473-81f6-66ec37e8b415
x-ms-diagnostics →2000001;reason="OAuth token submitted with the request can not be parsed.";error_category="invalid_token"
The token I get from https://login.microsoftonline.com/common/oauth2/token with the following body:
grant_type=authorization_code
redirect_uri=https://example.com/redirect-uri
client_id=XXXXXXXX-XXXX-XXXX-XXXXXXXXXXXX
client_secret=[my-client-secret]
code=[code-received-from-user-auth]
The token I receive from the above call, I send through the Authorization header to https://outlook.office.com/api/v2.0/me/events like so:
Authorization: Bearer [access-token]
The response status I get from this call is 401 Unauthorized, I get an empty body and the headers are like so:
Content-Length →0
Date →Thu, 17 Mar 2016 08:56:00 GMT
Server →Microsoft-IIS/8.0
WWW-Authenticate →Bearer client_id="00000002-0000-0ff1-ce00-000000000000", trusted_issuers="00000001-0000-0000-c000-000000000000#*", token_types="app_asserted_user_v1 service_asserted_app_v1", authorization_uri="https://login.windows.net/common/oauth2/authorize", error="invalid_token",Basic Realm="",Basic Realm=""
X-BEServer →DB4PR06MB522
X-BackEndHttpStatus →401
X-CalculatedBETarget →DB4PR06MB522.eurprd06.prod.outlook.com
X-DiagInfo →DB4PR06MB522
X-FEServer →AM3PR06CA022
X-Powered-By →ASP.NET
request-id →de1963bc-36df-4473-81f6-66ec37e8b415
x-ms-diagnostics →2000001;reason="OAuth token submitted with the request can not be parsed.";error_category="invalid_token"
Can you please tell me what I'm doing wrong?
I managed to fix the problem I had.
For future reference, the problem was that I wasn't telling the https://login.microsoftonline.com/common/oauth2/token endpoint what I needed the token for.
I had to provide a resource parameter with the base url of the resource I was going to interogate after.
In my case, it was https://outlook.office365.com.

Oauth2 for google api access in erlang

Hello I am writing code for oauth2 protocol for accessing google pub/sub api's . My code is as follows.
Method = post,
URL = "https://www.googleapis.com/oauth2/v1/userinfo",
Grant = "grant_type=authorization_code:params:oauth:grant-type:jwt-bearer&assertion=&
assertion=JWT_Token ",
Header = [{"Content-Type","application/x-www-form-urlencoded"}],
Body = [Grant],
Type = "application/x-www-form-urlencoded",
Options = [],
httpc:request(Method, {URL, Header, Type, Body}, [], Options).
I am getting following error when i send http request
Response{ok,{{"HTTP/1.1",404,"Not Found"},
[{"cache-control",
"no-cache, no-store, max-age=0, must-revalidate"},
{"date","Tue, 10 Nov 2015 22:27:36 GMT"},
{"pragma","no-cache"},
{"accept-ranges","none"},
{"server","GSE"},
{"vary","X-Origin"},
{"content-length","9"},
{"content-type","text/html; charset=UTF-8"},
{"expires","Fri, 01 Jan 1990 00:00:00 GMT"},
{"x-content-type-options","nosniff"},
{"x-frame-options","SAMEORIGIN"},
{"x-xss-protection","1; mode=block"},
{"alternate-protocol","443:quic,p=1"},
{"alt-svc","quic=\":443\"; p=\"1\"; ma=604800"}],
"Not Found"}}
I am almost sure the grant value i am sending is not correct.
I am not sure what parameter to pass in the URL and host parameters.
Please help

400 Bad Request when sending http post request to get token from auth code?

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

Resources