python spotify api getting access token and refresh token giving 400 - python

code = "AQB40N8OxnZAjvDcS8Yq6KhL0RqvKPKLCBnwtqHTnXQhMvwSyUgNUJlqyhww6qXoXYYlLZ7MmRP8Eu1XEeG9D2m3wEEdCJVyRos6brJiILl1ynKv-EL5G5dQ3vH418h-G948THH13ndUrrM-q0CDuYA06-aEpGlTk3vxK-g3bNBtS7jYSc82ToDsFgNAjL4WLPGs03Xm1j5I0zDZ7XUJASeqoCchy3-8"
import requests
import json
headers = {
'Authorization': 'Basic YTU5MWE4OTQ3ZGM4NDIzNDg2NzVlY2Y4MTk3N2M3MmI6YmRiZGJmNmY4Yjg5NDdlNGJhYzM5MzUxMTNmNmVlOGI=',
}
data = {
'grant_type': 'authorization_code',
'code': "AQB40N8OxnZAjvDcS8Yq6KhL0RqvKPKLCBnwtqHTnXQhMvwSyUgNUJlqyhww6qXoXYYlLZ7MmRP8Eu1XEeG9D2m3wEEdCJVyRos6brJiILl1ynKv-EL5G5dQ3vH418h-G948THH13ndUrrM-q0CDuYA06-aEpGlTk3vxK-g3bNBtS7jYSc82ToDsFgNAjL4WLPGs03Xm1j5I0zDZ7XUJASeqoCchy3-8",
'redirect_uri': 'http://localhost:8888'
}
response = requests.post('https://accounts.spotify.com/api/token', headers=headers, data=data)
print(response)
i dont understand why it doesnt work? what's wrong with it exactly? i followed https://developer.spotify.com/documentation/general/guides/authorization-guide/

I think it's because redirect_uri is not encoded.
try this way...
data = {
...
'redirect_uri' : 'http%3A%2F%2Flocalhost%3A8888'
}

Related

My youtube API integration says unathorized what is wrong with my code?

My code says unauthorized, what am I missing?
When I push the code I get <Response [401]> when I tried creating a json I got error unauthorized.
import requests
from secret import api_key
url = "https://www.googleapis.com/youtube/v3/search"
# channelId= "UCWv7vMbMWH4-V0ZXdmDpPBA"
headers = {
"Authorization": "Bearer" + api_key
}
params = {
"part":"snippet",
"forDeveloper":True,
"channelId": "UCWv7vMbMWH4-V0ZXdmDpPBA",
"maxResults": 49,
"order": "title",
"q":"python"
}
response = requests.get(url, headers=headers,params=params)
print(response)

Python Discord OAuth2 - Bad Request (Guild.Join)

Hi i'm trying to add a user to my guild using Discord's API.
The Scope I'm using is Guild.Join, Identify and Guilds. identify%20guilds%20guilds.join
The URL Is https://discordapp.com/api.
Here's my code:
#staticmethod
def add_to_guild(access_token, userID):
url = f"https://discordapp.com/api/guilds/{guildId that I cant show}/members/{userID}"
botToken = "<Bot Token I can't SHow haha>"
headers = {
"Authorization" : f"Bot {botToken}",
'Content-Type': 'application/json'
}
payload = {
'access_token' : access_token
}
response = requests.put(url=url, data=payload, headers=headers)
print(response.text)
when I call this method, I receive this error:
{"message": "400: Bad Request", "code": 0}
I've been through the Discord's Documentation countless of times and searched the internet to no avail.
Can someone please help ? Thanks.
you had to replace data to json
def add_to_guild(access_token, userID):
print(userID)
url = f"{Oauth.discord_api_url}/guilds/<GuildID>/members/{userID}"
headers = {
"Authorization" : "Bot <bottoken>",
"Content-Type": "application/json"
}
payload = {
'access_token' : str(access_token)
}
response = requests.put(url=url, json=payload, headers=headers)
print(response.text)
The userID is enough I am not sure why you want the payload or access token; Try to drop the payload part.
If you are trying to add a user, the user has to agree to grant you some permissions check here https://discord.com/developers/docs/topics/oauth2#shared-resources-oauth2-scopes so the token I think will be the "Bearer" tokens. Check the doc I've shared.

405 Error when trying to use the Send Message API endpoint

I'm using Python's request package with the following code:
APIKEY = "XXX"
url = 'https://services.kommunicate.io/rest/ws/message/v2/send HTTP/1.1'
myobj = {
'groupId': "xxx",
'message':'Hello',
"fromUserName":'yyy'
}
headers = {
'Api-Key':APIKEY,
}
response = requests.post(url, data = myobj,headers=headers)
And is giving me the following error:
'{"status":"error","errorResponse":[{"errorCode":"AL-MA-01","description":"method not allowed","displayMessage":"Request method \\u0027POST\\u0027 not supported"}],"generatedAt":1591385905404}'
What am I doing wrong?
There are few issues with the code.
1. HTTP/1.1 is not part of the URL.
2. In requests package, in order to pass a JSON to server, there are multiple ways to do.
a. Use json parameter provided in requests.post method for sending JSON data to the server, something like below code:
import requests
APIKEY = "XXX"
url = 'https://services.kommunicate.io/rest/ws/message/v2/send'
myobj = {
'groupId': "xxx",
'message': 'Hello',
"fromUserName": 'yyy'
}
headers = {'Api-Key': APIKEY}
response = requests.post(url, json=myobj, headers=headers)
b. In Headers add "Content-Type": "application/json" and then first dump json data to string and then send it to server.
import requests
import json
APIKEY = "XXX"
url = 'https://services.kommunicate.io/rest/ws/message/v2/send'
myobj = {
'groupId': "xxx",
'message': 'Hello',
"fromUserName": 'yyy'
}
headers = {
'Api-Key': APIKEY,
"Content-Type": "application/json"
}
myobj = json.dumps(myobj)
response = requests.post(url, data=myobj, headers=headers)
Also, Do check Difference between data and json parameters in python requests package

How to gain an Access Token

I am lost, I have no idea what should I do to get the access token. This is the code that I have tried, please help, please!
This is for Oauth2.0 token, and the API is Ocotoparse.
from octoparse import Octoparse as oct
import requests
host='https://advancedapi.octoparse.com/token'
url=host+'username={onlyfiii}&password={19970909huy}&grant_type=password'
r=requests.post(url)
username='onlyfiii'
password='19970909huy'
{
"access_token": "ABCD1234",
"token_type": "bearer",
"expires_in": 86399,
"refresh_token": "refresh_token"
}
I hope that's not your real password, if it is, change it immediately.
You need to send the request parameter as a form encoded POST request body, not as url parameters.
import requests
import json
url = 'https://advancedapi.octoparse.com/token'
payload = {
'username': 'onlyfiii',
'password': '19970909huy',
'grant_type': 'password',
}
r = requests.post(url, data=payload)
json_response = json.loads(r.text)
access_token = json_response['access_token']
See the API docs:
Request Content Type
application/x-www-form-urlencoded

Python Requests Microsoft Graph API Authentication

I'm having issues receiving the bearer token using Python for the Microsoft Graph API. Here is what I have so far:
import requests
import json
headers = {
'Content-Type': 'x-www-form-urlencoded',
'Authorization': 'Basic'
}
data = {
"grant_type": "client_credentials",
"client_id" :"<client_id>",
"client_secret": "<client_secret>",
"resource": "https://graph.microsoft.com"
}
r = requests.post('<token_address>', headers=headers, data=data)
print(r.text)
I have it worked in Postman, through x-www-form-urlencoded, but can't seem to get it working in Python. It returns The request body must contain the following parameter: 'grant_type'. I realize the problem probably has to do with needed the data converted, but I am not sure where to start.
You're sending some invalid headers in your request:
The Content-Type should be application/x-www-form-urlencoded not x-www-form-urlencoded.
You shouldn't be sending an Authorization header at all.
Technically, since requests.post sends data as form encoded by default, you can safely remove your headers from the request:
payload = {
'grant_type': 'client_credentials',
'client_id': '<client_id>',
'client_secret': '<client_secret>',
'resource': 'https://graph.microsoft.com',
}
r = requests.post('https://login.microsoftonline.com/common/oauth2/token', data=payload)
print(r.text)
I believe that OAuth expects the body to be URL-encoded, like this:
data = "grant_type=client_credentials"
+ "&client_id=<client_id>"
+ "&client_secret=<client_secret>"
+ "&resource=https://graph.microsoft.com"

Categories