0

i have found this interesting article here https://developer.byu.edu/docs/consume-api/use-api/oauth-20/oauth-20-python-sample-code in this article there is an example how to call an oauth2 api using authorization_code flow. the problem with this approach is that you need to open a new browser, get the code and paste in the script. i would open and get the code directly from python script. is it possible?

print "go to the following url on the browser and enter the code from the 
returned url: "
print "---  " + authorization_redirect_url + "  ---"
access_token = raw_input('access_token: ')
zanza67
  • 223
  • 4
  • 20
  • Not sure if this would help, but there but: https://stackoverflow.com/questions/31715119/how-can-i-open-a-website-in-my-web-browser-using-python – andreherberth Jun 20 '18 at 09:47
  • thanks for answering, the problem i have is that when i open the url, it is redirecting to provide credential and once authenticated and the app is approved it sends back with the code. how can i get the code???? and close the browser from script? – zanza67 Jun 20 '18 at 09:50
  • I am facing the similar challenge. Have you found a solution? – Liora Milbaum May 11 '19 at 14:26

1 Answers1

1

I have been battling with this same problem today and found that the following worked for me. You'll need:

  1. An API ID
  2. A secret key
  3. the access token url

I then used requests_oauthlib: https://github.com/requests/requests-oauthlib

from requests_oauthlib import OAuth2Session

# Both id and secret should be provided by whoever owns the api
myid = 'ID_Supplied'
my_secret = 'Secret_pass'

access_token_url = "https://example.com/connect/token" # suffix is also an example

client = BackendApplicationClient(client_id=myid)
oauth = OAuth2Session(client=client)
token = oauth.fetch_token(token_url=access_token_url, client_id=myid, 
client_secret=my_secret)

print(token)
Ryan Spear
  • 11
  • 1