Facebook OAuth 2 教程
在 Facebook APP 控制台 中设置一个新的 Web 应用程序客户端。当您获取 client_id
、client_secret
并注册一个回调 URL 后,您可以尝试下面的命令行交互式示例。
>>> # Credentials you get from registering a new application
>>> client_id = '<the id you get from facebook>'
>>> client_secret = '<the secret you get from facebook>'
>>> # OAuth endpoints given in the Facebook API documentation
>>> authorization_base_url = 'https://#/dialog/oauth'
>>> token_url = 'https://graph.facebook.com/oauth/access_token'
>>> redirect_uri = 'https://localhost/' # Should match Site URL
>>> from requests_oauthlib import OAuth2Session
>>> from requests_oauthlib.compliance_fixes import facebook_compliance_fix
>>> facebook = OAuth2Session(client_id, redirect_uri=redirect_uri)
>>> facebook = facebook_compliance_fix(facebook)
>>> # Redirect user to Facebook for authorization
>>> authorization_url, state = facebook.authorization_url(authorization_base_url)
>>> print('Please go here and authorize,', authorization_url)
>>> # Get the authorization verifier code from the callback url
>>> redirect_response = input('Paste the full redirect URL here:')
>>> # Fetch the access token
>>> facebook.fetch_token(token_url, client_secret=client_secret,
..> authorization_response=redirect_response)
>>> # Fetch a protected resource, i.e. user profile
>>> r = facebook.get('https://graph.facebook.com/me?')
>>> print(r.content)