本文整理汇总了Python中requests_oauthlib.OAuth2Session方法的典型用法代码示例。如果您正苦于以下问题:Python requests_oauthlib.OAuth2Session方法的具体用法?Python requests_oauthlib.OAuth2Session怎么用?Python requests_oauthlib.OAuth2Session使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类requests_oauthlib
的用法示例。
在下文中一共展示了requests_oauthlib.OAuth2Session方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: google_login
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def google_login():
# to avoid flask-login displaying the login error message
session.pop("_flashes", None)
next_url = request.args.get("next")
# Google does not allow to append param to redirect_url
# we need to pass the next url by session
if next_url:
session["google_next_url"] = next_url
google = OAuth2Session(GOOGLE_CLIENT_ID, scope=_scope, redirect_uri=_redirect_uri)
authorization_url, state = google.authorization_url(_authorization_base_url)
# State is used to prevent CSRF, keep this for later.
session["oauth_state"] = state
return redirect(authorization_url)
示例2: facebook_login
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def facebook_login():
# to avoid flask-login displaying the login error message
session.pop("_flashes", None)
next_url = request.args.get("next")
# Facebook does not allow to append param to redirect_uri
# we need to pass the next url by session
if next_url:
session["facebook_next_url"] = next_url
facebook = OAuth2Session(
FACEBOOK_CLIENT_ID, scope=_scope, redirect_uri=_redirect_uri
)
facebook = facebook_compliance_fix(facebook)
authorization_url, state = facebook.authorization_url(_authorization_base_url)
# State is used to prevent CSRF, keep this for later.
session["oauth_state"] = state
return redirect(authorization_url)
示例3: demo
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def demo():
"""Step 1: User Authorization.
Redirect the user/resource owner to the OAuth provider (i.e. SimpleLogin)
using an URL with a few key OAuth parameters.
"""
simplelogin = OAuth2Session(
client_id, redirect_uri="http://127.0.0.1:5000/callback"
)
authorization_url, state = simplelogin.authorization_url(authorization_base_url)
# State is used to prevent CSRF, keep this for later.
session["oauth_state"] = state
return redirect(authorization_url)
# Step 2: User authorization, this happens on the provider.
示例4: callback
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def callback():
""" Step 3: Retrieving an access token.
The user has been redirected back from the provider to your registered
callback URL. With this redirection comes an authorization code included
in the redirect URL. We will use that to obtain an access token.
"""
simplelogin = OAuth2Session(client_id, state=session["oauth_state"])
token = simplelogin.fetch_token(
token_url, client_secret=client_secret, authorization_response=request.url
)
# At this point you can fetch protected resources but lets save
# the token and show how this is done from a persisted token
# in /profile.
session["oauth_token"] = token
return redirect(url_for(".profile"))
示例5: _get_oauth_token
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def _get_oauth_token(self):
"""
Get Monzo access token via OAuth2 `authorization code` grant type.
Official docs:
https://monzo.com/docs/#acquire-an-access-token
:returns: OAuth 2 access token
:rtype: dict
"""
url = urljoin(self.api_url, '/oauth2/token')
oauth = OAuth2Session(
client_id=self._client_id,
redirect_uri=config.REDIRECT_URI,
)
token = oauth.fetch_token(
token_url=url,
code=self._auth_code,
client_secret=self._client_secret,
)
return token
示例6: session_from_client_secrets_file
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def session_from_client_secrets_file(client_secrets_file, scopes, **kwargs):
"""Creates a :class:`requests_oauthlib.OAuth2Session` instance from a
Google-format client secrets file.
Args:
client_secrets_file (str): The path to the `client secrets`_ .json
file.
scopes (Sequence[str]): The list of scopes to request during the
flow.
kwargs: Any additional parameters passed to
:class:`requests_oauthlib.OAuth2Session`
Returns:
Tuple[requests_oauthlib.OAuth2Session, Mapping[str, Any]]: The new
oauthlib session and the validated client configuration.
.. _client secrets:
https://developers.google.com/api-client-library/python/guide
/aaa_client_secrets
"""
with open(client_secrets_file, "r") as json_file:
client_config = json.load(json_file)
return session_from_client_config(client_config, scopes, **kwargs)
示例7: start_session
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def start_session(self, session):
"""
Starts a oauth session with the information stored in the browser session.
The OAuth2Session will take care of most of the work. Just have to
set a token_updater to update a token for BitBucket.
Input:
session: django.HttpRequest.session
"""
if self._token_key in session:
extra = {
'client_id' : self._client_id,
'client_secret' : self._secret_id,
'auth' : (self._client_id, self._secret_id),
}
def token_updater(token):
update_session_token(session, self, token)
return OAuth2Session(
self._client_id,
token=session[self._token_key],
auto_refresh_url=self._token_url,
auto_refresh_kwargs=extra,
token_updater=token_updater,
)
return None
示例8: start_session_for_user
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def start_session_for_user(self, user):
"""
Grabs the token for the user in the DB, then
starts a oauth session as that user.
Input:
user: models.GitUser: user to start the session for
Return:
OAuth2Session
"""
token = self.user_token_to_oauth_token(user)
extra = {
'client_id' : self._client_id,
'client_secret' : self._secret_id,
'auth' : (self._client_id, self._secret_id),
}
def token_updater(token):
update_user_token(user, token)
return OAuth2Session(
self._client_id,
token=token,
auto_refresh_url=self._token_url,
auto_refresh_kwargs=extra,
token_updater=token_updater,
)
示例9: sign_in
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def sign_in(self, request):
"""
Endpoint for the user signing in. Will start
the OAuth2 authentication process.
After this step the user will be redirected
to the server sign in page. After that, the server
will automatically call our registered callback,
which is "callback" above.
That will get access to the token that will be
used in all future communications.
"""
token = request.session.get(self._token_key)
request.session['source_url'] = request.GET.get('next', None)
if token:
messages.info(request, "Already signed in on %s" % self._hostname)
return self.do_redirect(request)
oauth_session = OAuth2Session(self._client_id, scope=self._scope, redirect_uri=self._redirect_uri)
authorization_url, state = oauth_session.authorization_url(self._auth_url)
request.session[self._state_key] = state
# Get rid of these keys on login
for key in self._addition_keys:
request.session.pop(key, None)
return redirect(authorization_url)
示例10: _get_oauth_session
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def _get_oauth_session(self):
"""Creates a new OAuth session
:return:
- OAuth2Session object
"""
return self._get_session(
OAuth2Session(
client_id=self.client_id,
token=self.token,
token_updater=self.token_updater,
auto_refresh_url=self.token_url,
auto_refresh_kwargs={
"client_id": self.client_id,
"client_secret": self.client_secret,
},
)
)
示例11: __init__
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def __init__(
self,
client_id: str,
client_secret: str,
redirect_uri: Optional[str] = None,
token: Optional[Dict[str, str]] = None,
token_updater: Optional[Callable[[str], None]] = None,
):
self.client_id = client_id
self.client_secret = client_secret
self.token_updater = token_updater
extra = {"client_id": self.client_id, "client_secret": self.client_secret}
self._oauth = OAuth2Session(
client_id=client_id,
token=token,
redirect_uri=redirect_uri,
auto_refresh_kwargs=extra,
token_updater=token_updater,
)
示例12: __init__
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def __init__(self, credentials):
self.credentials = credentials
self.token = {
'access_token': credentials.access_token,
'refresh_token': credentials.refresh_token,
'token_type': credentials.token_type,
'expires_in': str(int(credentials.token_expiry) - ts()),
}
oauth_client = WebApplicationClient(credentials.client_id,
token=self.token, default_token_placement='query')
self.client = OAuth2Session(
credentials.client_id,
token=self.token,
client=oauth_client,
auto_refresh_url='{}/oauth2/token'.format(NokiaAuth.URL),
auto_refresh_kwargs={
'client_id': credentials.client_id,
'client_secret': credentials.consumer_secret,
},
token_updater=self.set_token
)
示例13: __init__
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def __init__(self, client_id=None, client_secret=None, client=None,
auto_refresh_url=None, auto_refresh_kwargs=None, scope=None,
redirect_uri=None, token=None, state=None, token_updater=None,
**kwargs):
self.session = OAuth2Session(
client_id=client_id,
client=client,
auto_refresh_url=token_url,
scope=scope,
redirect_uri=redirect_uri,
token=token,
state=state,
token_updater=token_updater,
**kwargs
)
self.client_secret = client_secret
self.base_url = "https://api.smashrun.com/v1"
示例14: callback
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def callback(self, auth_storage):
linkedin = OAuth2Session(self._client_id, state=auth_storage["oauth_state"], redirect_uri=web.ctx.home + self._callback_page)
try:
linkedin.fetch_token(token_url, include_client_id=True, client_secret=self._client_secret,
authorization_response=web.ctx.home + web.ctx.fullpath)
r = linkedin.get('https://api.linkedin.com/v2/me?projection=(id,localizedFirstName,localizedLastName)')
profile = json.loads(r.content.decode('utf-8'))
r = linkedin.get('https://api.linkedin.com/v2/clientAwareMemberHandles?q=members&projection=(elements*(primary,type,handle~))')
result = json.loads(r.content.decode('utf-8'))
for contact in result["elements"]:
if contact["type"] == "EMAIL":
profile["emailAddress"] = contact["handle~"]["emailAddress"]
break
return str(profile["id"]), profile["localizedFirstName"] + " " + profile["localizedLastName"], profile["emailAddress"], {}
except Exception as e:
return None
示例15: share
# 需要导入模块: import requests_oauthlib [as 别名]
# 或者: from requests_oauthlib import OAuth2Session [as 别名]
def share(self, auth_storage, course, task, submission, language):
facebook = OAuth2Session(self._client_id, state=auth_storage["oauth_state"], redirect_uri=web.ctx.home + self._callback_page)
if facebook:
r = facebook.post("https://graph.facebook.com/me/objects/website",
{
"object": json.dumps({
"og:title": _("INGInious | {course} - {task}").format(
course=course.get_name(language),
task=task.get_name(language)
),
"og:description": _("Check out INGInious course {course} and beat my score of {score}% on task {task} !").format(
course=course.get_name(language),
task=task.get_name(language),
score=submission["grade"]
),
"og:url": web.ctx.home + "/course/" + course.get_id() + "/" + task.get_id(),
"og:image": "http://www.inginious.org/assets/img/header.png"})
})
result = json.loads(r.content.decode('utf-8'))
return "id" in result