How to gain an Access Token - python

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

Related

Python Citrix Sharefile Token Request giving 400 error

I am trying to make a simple request to get the access token from Citrix ShareFile, but it's throwing 400 error.
I am going exactly as it's mentioned in the documentation, except changing Python2 code with HTTPLib, with Python3 code with Requests. The code is:
url = 'https://{my_domain}.sharefile.com/oauth/token'
headers = {'Content_Type': 'application/x-www-form-urlencoded'}
params = {'grant_type':'password', 'client_id':my_client_id, 'client_secret':my_client_secret, 'username':my_username, 'password':my_password}
response = requests.post(url, params=params, headers = headers)
print(response.status_code, response.reason)
I get the following response:
400 Bad Request
I also added urllib.parse.urlencode to params, but am still getting the same response error
response = requests.post(url, params=urllib.parse.urlencode(params), headers = headers)
Request guidance on what am I doing wrong. TIA
It could be the password, in the context of Sharefile it means app_password and not the password used to login to website. Or response_type to 'code'.
SF Auth is through OAuth2 with a GrandType: OAuth2 Password Grant
This way works for me:
url = 'https://{my_domain}.sharefile.com/oauth/token'
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {
'response_type': 'code',
'grant_type': 'password',
'client_id': '<YOUR CLIENT ID>',
'client_secret': '<YOUR SECRET>',
'username': '<USERNAME>',
'password': '<APP_PASSWORD>' # not regular password to login using web
}
response = requests.post(url, data=data, headers=headers)
Response contains token and refresh token.
When I add content-type my issue solved.
Check and add valid content-type

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.

python spotify api getting access token and refresh token giving 400

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

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"

Why do i get "Method not Allowed (HTTP CODE 405)" on sending post request?

I tried to get successful login post request, but don't understand why do i always get status code 405. I checked many tutorials and all of them worked good for some examples.
import requests
import urllib.parse
def logintest(usr, pswd):
link = 'link'
headers = { 'Content-Type' : 'application/x-www-form-urlencoded',
'Server' : 'Apache-Coyote/1.1',
'Transfer-Encoding' : 'chunked'
}
payload = urllib.parse.urlencode({
'username': usr,
'password': pswd
})
responseget = requests.get(link)
print (responseget.status_code, responseget.reason)
if responseget.status_code == 200:
responsepost = requests.post(link, params = payload, headers = headers)
print (responsepost.status_code, responsepost.reason)
logintest('username', 'password')
Because the elibrary/home endpoint does not support POST requests. You probably want to send the request to http://81.180.75.144:8080/elibrary/auth/login, like the system does.

Categories