import requests
url = "xxxxy/v3/oauth2/token"
payload = {
"grant_type": "client_credentials",
"client_id": "xxxxx",
"client_secret": "xxxxx"
}
headers = {
"Accept": "application/json",
"Content-Type": "application/json"
}
response = requests.request("POST", url, json=payload, headers=headers)
print(response.text)
and my output is like this
Output: In output we can see access_token, token type and time to expire this key. I want output only the text that is in "access_token".
}
"tokenInfo": {
"access_token": "XFGXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
"expires_in": 3600,
"token_type": "Bearer"
}
}
How about the following?
# get json response and navigate to access token
json = response.json()
access_token = json["tokenInfo"]["access_token"]
print(access_token)
Related
I need to get access token to moving in the site with request
i do the same thigs that writen in the documentation:
{
"grant_type": "",
"client_id": "",
"client_secret": "",
"scope": "v2 read write"
}
My:
payload = {
"grant_type": "",
"client_id": f"{client_id}",
"client_secret": f"{client_secret}",
"scope": "read write v2"
}
headers = {
'accept': '*/*',
'accept-language': 'ru',
'content-type': 'application/json',
'user-agent': f'{fake_useragent}'
}
resp = requests.post(url=url,headers=headers,json=payload)
print(resp.json())
payload = {
"grant_type": "",
"client_id": f"{client_id}",
"client_secret": f"{client_secret}",
"scope": "read write v2"
}
headers = {
'accept': '*/*',
'accept-language': 'ru',
'content-type': 'application/json',
'user-agent': f'{fake_useragent}'
}
resp = requests.post(url=url,headers=headers,json=payload)
print(resp.json())
and than:
{'error': 'invalid_request', 'error_description': 'The grant type was not specified in the request', 'error_human_title': 'Некорректные данные.'}
As response client will receive:
{
"access_token": "454c3cd39a980500e9b787e03d53ec5c320f644b",
"expires_in": 86400,
"token_type": "bearer",
"scope": "v2 read write",
"refresh_token": "12a13560163da7901d7f74c89fc5df3ea7625942"
}
also tested on postman but got the same error, but in the develp. it's look like this enter image description here
What am i doing wrong?
postman
And also i tried that way
{
"grant_type": "authorization_code",
"client_id": "",
"client_secret": "",
"code": "",
"scope": "v2 read write",
"redirect_uri": "http://myadomain.com/auth/connect"
}
and than postman
Link to the documentation - https://developer.olx.ua/api/doc#section/Authentication/
I am trying to make an API call to GPT-3 but I am getting an error (Bad request 400). Here is my code:
url = "https://api.openai.com/v1/engines/gpt-3/jobs"
headers = {
"Content-Type": "application/json",
"Authorization": "sk-apikey"
}
data = {
"model": "text-davinci-002",
"prompt": "Correct this to standard English : Who are you",
"max_tokens": 60
}
response = requests.post(url, headers=headers, data=json.dumps(data))
Try changing the url and fixing the Authorization header...
url = "https://api.openai.com/v1/completions"
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {api_key}"
}
data = {
"model": "text-davinci-002",
"prompt": "Correct this to standard English : Who are you \n",
"max_tokens": 60
}
response = requests.post(url, headers=headers, data=json.dumps(data))
response.json()
I want to pass a randomly generated password to my payload. This is what I have-
import requests
import json
import random
import string
#generate 12 character password
alphabet = string.ascii_letters + string.digits + '!##%^*()'
newpassword = ''.join(random.choice(alphabet) for i in range(12))
url = "https://www.example.com"
payload = "{\n \"id\": 123,\n \"name\": \"John\",\n \"itemValue\": "+newpassword+" \n}"
headers = {
'Content-Type': 'application/json',
}
response = requests.put(url, headers=headers, data=json.dumps(payload))
print(response.text)
I'm not getting the desired output because it is not taking the new password string correctly.
Please advise.
your payload is already a JSON string, so there's no need to call json.dumps(payload) , just use:
response = requests.put(url, headers=headers, data=payload)
calling json.dumps is needed when your payload is not a JSON string. for example:
payload = {"id": 123, "name": "John", "itemValue": newpassword}
requests.put(url, headers=headers, data=json.dumps(payload))
Also, you need to surround newpassword with quotes:
payload = "{\n \"id\": 123,\n \"name\": \"John\",\n"
payload += "\"itemValue\": \"" + newpassword + "\" \n}"
In order to test it I changed your url to "https://httpbin.org/put" and it works fine. the output is:
{
"args": {},
"data": "{\n \"id\": 123,\n \"name\": \"John\",\n \"itemValue\": \"Vj1YsqPRF3RC\" \n}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "69",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.25.1",
"X-Amzn-Trace-Id": "Root=1-601e4aa5-2ec60b9839ba899e2cf3e0c9"
},
"json": {
"id": 123,
"itemValue": "Vj1YsqPRF3RC",
"name": "John"
},
"origin": "13.110.54.43",
"url": "https://httpbin.org/post"
}
I'm trying to create an object through back4app for class sound
the issue which am having that am unable to upload the file.
code used:
import requests
headers = {
"X-Parse-Application-Id": "hidden",
"X-Parse-REST-API-Key": "hidden",
}
data = {
"audio": {
"__type": open("a.mp3", 'rb'),
"name": "a.mp3"
},
"displayText": "test"
}
def main(url):
with requests.Session() as req:
req.headers.update(headers)
r = req.post(url, data=data)
print(r.text)
main("https://parseapi.back4app.com/classes/sounds")
Output:
{"code":111,"error":"schema mismatch for sounds.audio; expected File but got Array"}
You first need to upload the file:
import json,httplib
connection = httplib.HTTPSConnection('parseapi.back4app.com', 443)
connection.connect()
connection.request('POST', '/files/a.mp3', open('a.mp3', 'rb').read(), {
"X-Parse-Application-Id": "${APPLICATION_ID}",
"X-Parse-REST-API-Key": "${REST_API_KEY}",
"Content-Type": "audio/mpeg"
})
result = json.loads(connection.getresponse().read())
Then create the object:
connection.request('POST', '/classes/sounds', json.dumps({
"displayText": "test",
"audio": {
"name": result["name"],
"url:": result["url"],
"__type": "File"
}
}), {
"X-Parse-Application-Id": "${APPLICATION_ID}",
"X-Parse-REST-API-Key": "${REST_API_KEY}",
"Content-Type": "application/json"
})
connection.getresponse().read()
I'm trying to translate Curl to Python, but I'm getting something wrong. Please help.
CURL:
body=$(cat << EOF
{
"order": {
"units": "-100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
EOF
)
curl \
-X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <AUTHENTICATION TOKEN>" \
-d "$body" \
"https://api-fxtrade.oanda.com/v3/accounts/<ACCOUNT>/orders"
PYTHON:
import requests
import json
def market_buy():
header = {"Accept": "application/json",
"Authorization": "Bearer <my auth code>"
}
data = {
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
url = "https://api-fxtrade.oanda.com/v3/accounts/<myaccount>/orders"
r = requests.post(url, data=data, headers=header)
print(r.text)
market_buy()
Error Message:
{"errorMessage":"Insufficient authorization to perform request."}
I've double-checked my credentials. I'm thinking something is wrong with the code.
A direct translation from cURL to Python requests would be this:
from requests import post
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer <AUTHENTICATION TOKEN>",
}
data = {
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT",
}
}
post(
"https://api-fxtrade.oanda.com/v3/accounts/<myaccount>/orders",
headers=headers,
data=data,
)
I guess you are missing the SSL cert verification and basic authentication.
You can turn off SSL cert verification with the verify flag, and use basic authentication by specifying auth.
from requests.auth import HTTPBasicAuth
import requests
import json
def market_buy():
header = {"Accept": "application/json",
"Authorization": "Bearer <my auth code>"
}
data = {
"order": {
"units": "100",
"instrument": "EUR_USD",
"timeInForce": "FOK",
"type": "MARKET",
"positionFill": "DEFAULT"
}
}
url = "https://api-fxtrade.oanda.com/v3/accounts/<myaccount>/orders"
r = requests.post(url, data=data, headers=header, auth=HTTPBasicAuth('admin', 'admin'), verify=False)
print(r.text)
market_buy()
I guess the above should work.