Python Citrix Sharefile Token Request giving 400 error - python

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

Related

Postman API get access token

I am not trying to get an access token to any other website using postman, I am trying to get an access token for the actual Postman website!!
I have a script that I am working to implement for Azure and I need to use postman to send a POST request and download the response body. Based on my research, to be able to use the POST request to download the response body, I need to pass Postman an access token as a parameter for my download function.
This is my auth method to get the access token for postman:
def authenticate_postman(api_key, username, password):
''' Authenticate into Postman API and get a valid access token
'''
global access_token
url = "https://api.postman.com/oauth/token"
headers = {
"X-Api-Key": api_key,
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"grant_type": "password",
"username": username,
"password": password
}
response = requests.post(url, headers=headers, data=data)
if response.status_code != 200:
print(response.json())
raise Exception("Failed to authenticate. HTTP Error {}".format(response.status_code))
access_token = response.json()["access_token"]
return access_token
api_key, username, password come from os.environ
I get this error:
{'error': {'name': 'notFound', 'message': 'Requested resource not found'}}
I am not sure why it doesn't work or how I can fix this. Any help would be appreciated!

Request API POST

I'm having a problem with a Python request, where I pass the body url to the API data.
Note: I have a Node.js project with TypeScript that works normally, prints to the screen and returns values. However if I try to make a request in Python it doesn't work with an error 401.
Below is an example of how the request is made in Python. can you help me?
import requests
url = 'https://admins.exemple'
bodyData = {
'login': 'admins',
'pass': 'admin',
'id': '26' }
headers = {'Content-Type': 'application/json'}
resp = requests.post(url, headers=headers, data=bodyData)
data = resp.status_code
print(data)
Please dump a dict to a json string as follows:
import json
resp = requests.post(url, headers=headers, data=json.dumps(bodyData))
You also can pass your dict to json kwarg
resp = requests.post(url, headers=headers, json=bodyData)
It will set Content-Type: application/json and dump dict to json automatically
You are not correctly authenticating with the server. Usually, you need to send the username and password to a "sign in" route which will then return a token. Then you pass the token with other requests to get authorization. Since I don't know any details about your server and API, I can't provide any more details to help you out.

How to Resolve Status 401 in HTTP Post Request Python?

I am trying to make an http post request in Python. The http that I am posting to requires an authorization. For security, I have replaced the real url, data and authorization. When I run the code below with the correct url, data body and authorization I get status 401. That would imply that the authorization is not correct. Is there another way I should be adding authorization, headers and body to my http post request so that the post is successful? The authorization password is correct and works in Postman, so it's contents is not the issue--I'm wondering if the format is. Thanks in advance!
import requests
from requests.auth import HTTPBasicAuth
def post():
url = 'http://somelongurl'
headers = {'Content-Type': 'application/json'}
auth = HTTPBasicAuth('Authorization', 'PASSWORD')
data = {"ProductID" : "ABCDEFG",
"ClientID": "10000",
"SerialNumber": "1234567890"}
req = requests.post(url, headers=headers, data=data, auth=auth)
return req
This was resolved by including the authorization in the headers and changing the data as type json as follows: https://postimg.cc/3y7B6fvL

How to properly authenticate using Python requests via a POST request

I am using the REST API for iObeya, I wish to use Python Requests but currently I cannot authenticate with the server. The documentation states that you can authenticate using a POST request and upon return, you should get a cookie called 'JSESSIONID' when the auth is correct.
So far I have this:
url = 'https://link-to.com/iobeya/'
auth_url = 'https://link-to.com/iobeya/j_spring_security_check'
headers = {"content-type": "application/x-www-form-urlencoded; charset=utf-8"}
session = requests.Session()
session.auth = ("username", "password")
auth = session.post(url, verify=False)
r = session.get(url, verify=False, headers=headers)
print(r.status_code)
print(r.headers)
print(r.cookies)
The return of cookies is null. Is this the right way to be doing a auth request using a POST method?
Here is the page describing how the auth API works:
It just wants you to make a normal POST with username and password.
auth_url = 'https://link-to.com/iobeya/j_spring_security_check'
session = requests.Session()
auth = session.post(auth_url, data={'username': username, 'password': password})

How to update access token using refresh token in you tube api?

I have tried this to update my access token
import urllib
endpoint='https://accounts.google.com/o/oauth2/token'
data={'client_id':'25********15-6*********************7f.apps.googleusercontent.com','client_secret':'4********Pj-K*****x4aM','refresh_token':'1/tP************************O_XclU','grant_type':'refresh_token'}
encodedData=urllib.urlencode(data)
from httplib2 import Http
h = Http()
resp, content = h.request(endpoint, "POST", encodedData)
But got the error message
'{\n "error" : "invalid_request",\n "error_description" : "Required parameter is missing: grant_type"\n}'
You should specify the headers in your request like this:
resp, content = h.request(uri=endpoint,
method="POST",
body=encodedData,
headers={'Content-type': 'application/x-www-form-urlencoded'})
old-refresh_token is the refresh token you have
CLIENT_ID,CLIENT_SECRET are the credentials you can find those in google developers console
http = httplib2.Http()
TOKEN_URL = "https://accounts.google.com/o/oauth2/token"
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
parameters = urllib.urlencode({'refresh_token': old-refresh_token, 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, 'grant_type': 'refresh_token'})
resp, response_token = http.request(TOKEN_URL, method='POST', body=parameters, headers=headers)
token_data = json.loads(response_token)
access_token = token_data['access_token']
the variable access_token now holds your access token
try it out

Categories