#!/usr/bin/env python# -*- coding: utf-8 -*-fromrequests_oauthlibimportOAuth2SessionclassClientSecrets:''' The structure of this class follows Google convention for `client_secrets.json`: https://developers.google.com/api-client-library/python/guide/aaa_client_secrets Bitbucket does not emit this structure so it must be manually constructed. '''client_id="Shown as Key in the Bitbucket Cloud UI"client_secret="Shown as Secret in the Bitbucket Cloud UI"redirect_uris=["https://localhost"# Used for testing.]auth_uri="https://bitbucket.org/site/oauth2/authorize"token_uri="https://bitbucket.org/site/oauth2/access_token"server_base_uri="https://api.bitbucket.org/"defmain():c=ClientSecrets()# Fetch a request tokenbitbucket=OAuth2Session(c.client_id)# Redirect user to Bitbucket for authorizationauthorization_url=bitbucket.authorization_url(c.auth_uri)print('Please go here and authorize: {}'.format(authorization_url[0]))# Get the authorization verifier code from the callback urlredirect_response=raw_input('Paste the full redirect URL here:')# Fetch the access tokenbitbucket.fetch_token(c.token_uri,authorization_response=redirect_response,username=c.client_id,password=c.client_secret)# Fetch a protected resource, i.e. user profiler=bitbucket.get(c.server_base_uri+'1.0/user')print(r.content)if__name__=='__main__':main()
Comments (2)
范姜翊庭
Hi,
I run this code but have some problems.
Do you have any idea?
Traceback (most recent call last):
File "bboauth.py", line 42, in <module>
main()
File "bboauth.py", line 30, in main
redirect_response = raw_input('Paste the full redirect URL here:')
NameError: name 'raw_input' is not defined
Harshil shahAccount Deactivated
change raw_input to just input. Python 3 does not support raw_input
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.
Hi, I run this code but have some problems. Do you have any idea?
change raw_input to just input. Python 3 does not support raw_input