why post request has different result? - python

I have use curl in linux to post data to asana and its work fine.
curl -H "Authorization: Bearer <mytoken>" https://app.asana.com/api/1.0/tasks/101/stories -d "text=hello world"
but if I use requests library for python the result is 400
response = requests.post("https://app.asana.com/api/1.0/tasks/101/stories", auth=(self.asana.token_uri, ""), params = "text=repo_name")

You need to add the same "Authorization: Bearer" token header to your request in python. Similar to this answer:
https://stackoverflow.com/a/29931730/6080374

The auth argument to requests methods produces a Authorization: Basic header, not a Authorization: Bearer header.
Set a headers dictionary with a manually created Authorization header instead. POST data (the -d command line switch for curl) should be passed as a dictionary to the data argument:
response = requests.post(
"https://app.asana.com/api/1.0/tasks/101/stories",
headers={'Authorization': 'Bearer {}'.format(self.asana.token_uri),
data={'text': 'repo_name'})

Related

Using python requests with an API key that requires spaces

I need to POST data to an API endpoint using the python requests library with application/json content-type but am getting errors due to the API key having a space in it.
API Key format:
Authorization: Token d6bf96a81a58bf6e99ad1f819b244242797c0c07
Successful curl request:
curl -k -H "Authorization: Token d6bf96a81a58bf6e99ad1f819b244242797c0c07" "https://myurlhere.com/api/status/" --data "status=Good&value=foobar"
Failed Python:
import requests
url="https://myurlhere.com/api/status/"
headers={"Authorization": "Token d6bf96a81a58bf6e99ad1f819b244242797c0c07","content-type":"application/json}
data="status=Good&value=foobar"
requests.post(url, headers=headers, data=data)
The python request returns a 403, additional debugging shows "Authentication details were not provided."
It appears as if the space is causing issues due to being a json object? Is there any clean way to work around this? I must use the application/json type as I use the same program with a lot more code for other api requests which require the application/json type.

Receive Indeed API Access Token [Python]

I am trying to get the Indeed vacanties of my company via API with python. I am following https://developer.indeed.com/docs/authorization/3-legged-oauth and https://mathiashaentjens.medium.com/how-do-you-extract-data-using-the-indeed-api-and-build-your-own-indeed-campaign-reporting-8127252ef073.
I create Indeed API keys and recevive the Authorization Code. But i couldnt get Access Token. I send the same POST as documents via curl and python requests but i got this error;
{'error_description': 'Your request might include sensitive information passed in the URL query string. Parameters must be passed in the HTTP request body using the application/x-www-form-urlencoded format (See https://datatracker.ietf.org/doc/html/rfc6749#section-4.1.3). For increased security, we recommend that you periodically rotate your application secret at https://secure.indeed.com/account/apikeys.', 'error': 'invalid_request'}
My python code is like;
headers = {'content-type': 'application/x-www-form-urlencoded','accept':'application/json'}
payload = {'code':'XXXX', 'client_id':'XXXX', 'client_secret':'XXXX', 'redirect_uri': 'http://localhost', 'grant_type':'authorization_code'}
response = requests.post('https://apis.indeed.com/oauth/v2/tokens', params=urllib.parse.urlencode(payload), headers=headers)
response.json()
and via command line;
curl -X POST -H "Content-Length: 0" -H "Content-Type: application/x-www-form-urlencoded" -H "Accept: application/json" "https://apis.indeed.com/oauth/v2/tokens?code=XXXX&client_id=XXXX&client_secret=XXXX&redirect_uri=http://localhost&grant_type=authorization_code"
Is there anyone familiar with this error?

How can I get the data from an API using Python Request Post?

I'm trying to retrive some data from apptopia but I'm finding it pretty tricky (due to my lack of experience). In their authentication page: https://dev.apptopia.com/#authentication there are some instructions, but I just can't make it work.
I need a client and a secret (these bellow are not mine but the ones on the company's site)
client: JFqXPDhiLuvY
secret: L2nerprCksacBoFzUqtfHz8v
And I must use those information in order to obtain a Session token via HTTPS POST request:
curl -X "POST" "https://integrations.apptopia.com/api/login" \
-H "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client=<client>" \
--data-urlencode "secret=<secret>"
I just don't know how to do it. I tried using the answen on this post: Python Request Post with param data but it didn't work. Could someone help me please? Thanks!
Did you try passing credentials as data in your request?
import requests
headers = {
'Content-Type': 'application/x-www-form-urlencoded',
}
data = {
'client':your_client,
'secret':your_secret'
}
response = requests.post('https://integrations.apptopia.com/api/login', headers=headers, data=data)

Getting 404 when using requests, but curl works fine

I'm using requests to pull down some JSON from a 3rd party portal.
Whenever i use requests i get a '{"Message":"Path not found"}' error
however running the command with cURL works fine. Here is the cURL command:
curl https://blah.com/api/v1/nodes --header 'Authorization: Bearer my-long-token-base64'
Trying to do this with python3 / requests i have:
#convert token to base64
base64_token = base64.b64encode(bytes(access_token, 'utf-8'))
#convert to str
base64_token = base64_token.decode("utf-8")
api = 'https://blah.com/api/v1/nodes'
headers = {'Authorization': 'Bearer ' + base64_token,
'Accept': 'application/json'}
nodes = requests.post(api, headers=headers)
whenever i run this the response is '{"Message":"Path not found"}'
I thought it might be something to do with the base64 token (which is required) but pretty sure i've got that part right because otherwise i get a 401.
any thoughts?

Get Twitch channel ID from Python

It just says {"error":"Unauthorized","status":401,"message":"error getting authorization token"}, don't know why as I can write on chat with my token.
Here's the code:
url = "https://api.twitch.tv/kraken/channel"
channel_id = urllib.request.Request(url)
channel_id.add_header("Client-ID", CLIENT_ID)
#MY_OAUTH defined as MY_OAUTH = "oauth:123blablabla"
channel_id.add_header("Authorization: OAuth", MY_OAUTH")
response = urllib.request.urlopen(channel_id)
tmpJSON = json.loads(response.read())
EDIT: Here's the Pastebin of get_channel_id function: https://pastebin.com/Jm0EuWk9
It seems that your Authorization header is ill-formed. Supposing that MY_OAUTH as your access token, I believe you meant to write :
channel_id.add_header("Authorization", "OAuth " + MY_OAUTH)
Indeed, the Twitch-API authentication documentation recommends performing the following request when passing the access token in the HTTP header:
curl -H "Authorization: OAuth [access token]" https://api.twitch.tv/kraken/
As you can see, the header needs to be set as Authorization: OAuth [access token], instead of Authorization: OAuth: [access token].

Categories

Resources