本文整理汇总了Python中oauth2.Token方法的典型用法代码示例。如果您正苦于以下问题:Python oauth2.Token方法的具体用法?Python oauth2.Token怎么用?Python oauth2.Token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类oauth2
的用法示例。
在下文中一共展示了oauth2.Token方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: login
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def login(self, username, password):
'''Authenticate using XAuth variant of OAuth.
:param str username: Username or email address for the relevant account
:param str password: Password for the account
'''
response = self.request(
ACCESS_TOKEN,
{
'x_auth_mode': 'client_auth',
'x_auth_username': username,
'x_auth_password': password
},
returns_json=False
)
token = dict(parse_qsl(response['data'].decode()))
self.token = oauth.Token(
token['oauth_token'], token['oauth_token_secret'])
self.oauth_client = oauth.Client(self.consumer, self.token)
示例2: __init__
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def __init__(self, key, secret, email, password, cartodb_domain, host='carto.com', protocol='https', proxy_info=None, *args, **kwargs):
super(CartoDBOAuth, self).__init__(cartodb_domain, host, protocol, *args, **kwargs)
self.consumer_key = key
self.consumer_secret = secret
consumer = oauth.Consumer(self.consumer_key, self.consumer_secret)
client = oauth.Client(consumer, proxy_info=proxy_info)
client.set_signature_method = oauth.SignatureMethod_HMAC_SHA1()
params = {}
params["x_auth_username"] = email
params["x_auth_password"] = password
params["x_auth_mode"] = 'client_auth'
# Get Access Token
access_token_url = ACCESS_TOKEN_URL % {'user': cartodb_domain, 'domain': host, 'protocol': protocol}
resp, token = client.request(access_token_url, method="POST", body=urllib.urlencode(params))
access_token = dict(urlparse.parse_qsl(token))
token = oauth.Token(access_token['oauth_token'], access_token['oauth_token_secret'])
# prepare client
self.client = oauth.Client(consumer, token)
示例3: oauth_login
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def oauth_login(self, url, oauth_token, oauth_token_secret,
consumer_key='anonymous', consumer_secret='anonymous'):
"""Authenticate using the OAUTH method.
This only works with IMAP servers that support OAUTH (e.g. Gmail).
"""
if oauth_module:
token = oauth_module.Token(oauth_token, oauth_token_secret)
consumer = oauth_module.Consumer(consumer_key, consumer_secret)
xoauth_callable = lambda x: oauth_module.build_xoauth_string(url,
consumer,
token)
return self._command_and_check('authenticate', 'XOAUTH',
xoauth_callable, unpack=True)
else:
raise self.Error(
'The optional oauth2 package is needed for OAUTH authentication')
示例4: callback
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def callback():
global req_token
global consumer
config = read_config()
oauth_verifier = request.args.get('oauth_verifier', '')
token = oauth.Token(req_token.oauth_token,
req_token.oauth_token_secret)
token.set_verifier(oauth_verifier)
client = oauth.Client(consumer, token)
resp, content = client.request(config.access_token_url, 'POST')
access_token = dict(parse_qsl(content.decode('utf-8')))
with open('.twitterauth', 'w') as req_auth:
file_content = yaml.dump(access_token, default_flow_style=False)
req_auth.write(file_content)
return 'All set! You can close the browser window and stop the server.'
示例5: make_request
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def make_request(self, url, method="GET", body="", headers=None):
"""Makes a request to the TradeKing API."""
consumer = Consumer(key=TRADEKING_CONSUMER_KEY,
secret=TRADEKING_CONSUMER_SECRET)
token = Token(key=TRADEKING_ACCESS_TOKEN,
secret=TRADEKING_ACCESS_TOKEN_SECRET)
client = Client(consumer, token)
body_bytes = body.encode("utf-8")
self.logs.debug("TradeKing request: %s %s %s %s" %
(url, method, body_bytes, headers))
response, content = client.request(url, method=method,
body=body_bytes,
headers=headers)
self.logs.debug("TradeKing response: %s %s" % (response, content))
try:
return loads(content)
except ValueError:
self.logs.error("Failed to decode JSON response: %s" % content)
return None
示例6: use_access_token
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def use_access_token(oauth_token, oauth_secret):
consumer = oauth.Consumer(key='9fdce0e111a1489eb5a42eab7a84306b', secret='liqutXmqJpKmetfs')
token = oauth.Token(oauth_token, oauth_secret)
oauth_request = oauth.Request.from_consumer_and_token(consumer, http_url=RESOURCE_URL)
oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
# get the resource
response = requests.get(RESOURCE_URL, headers=oauth_request.to_header())
return response.text
示例7: get_access_token
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def get_access_token(oauth_token, oauth_secret, oauth_verifier):
consumer = oauth.Consumer(key='9fdce0e111a1489eb5a42eab7a84306b', secret='liqutXmqJpKmetfs')
token = oauth.Token(oauth_token, oauth_secret)
token.set_verifier(oauth_verifier)
oauth_request = oauth.Request.from_consumer_and_token(consumer, token, http_url=ACCESS_TOKEN_URL)
oauth_request.sign_request(oauth.SignatureMethod_HMAC_SHA1(), consumer, token)
response = requests.get(ACCESS_TOKEN_URL, headers=oauth_request.to_header())
access_token = dict(urllib.parse.parse_qsl(response.content.decode('utf8')))
return access_token
示例8: authenticate
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def authenticate(self, url, consumer, token):
if consumer is not None and not isinstance(consumer, oauth2.Consumer):
raise ValueError("Invalid consumer.")
if token is not None and not isinstance(token, oauth2.Token):
raise ValueError("Invalid token.")
self.docmd('AUTH', 'XOAUTH %s' % \
base64.b64encode(oauth2.build_xoauth_string(url, consumer, token)))
示例9: authenticate
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def authenticate(self, url, consumer, token):
if consumer is not None and not isinstance(consumer, oauth2.Consumer):
raise ValueError("Invalid consumer.")
if token is not None and not isinstance(token, oauth2.Token):
raise ValueError("Invalid token.")
imaplib.IMAP4_SSL.authenticate(self, 'XOAUTH',
lambda x: oauth2.build_xoauth_string(url, consumer, token))
示例10: prepare_request
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def prepare_request(url, url_params):
reqconfig = read_reqauth()
config = read_config()
token = oauth.Token(
key=reqconfig.oauth_token,
secret=reqconfig.oauth_token_secret)
consumer = oauth.Consumer(
key=config.consumer_key,
secret=config.consumer_secret)
params = {
'oauth_version': "1.0",
'oauth_nonce': oauth.generate_nonce(),
'oauth_timestamp': str(int(time.time()))
}
params['oauth_token'] = token.key
params['oauth_consumer_key'] = consumer.key
params.update(url_params)
req = oauth.Request(method="GET", url=url, parameters=params)
signature_method = oauth.SignatureMethod_HMAC_SHA1()
req.sign_request(signature_method, consumer, token)
return req.to_url()
示例11: __init__
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def __init__(self, consumer, token, user_agent):
"""
consumer - An instance of oauth.Consumer.
token - An instance of oauth.Token constructed with
the access token and secret.
user_agent - The HTTP User-Agent to provide for this application.
"""
self.consumer = consumer
self.token = token
self.user_agent = user_agent
self.store = None
# True if the credentials have been revoked
self._invalid = False
示例12: step2_exchange
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def step2_exchange(self, verifier):
"""Exhanges an authorized request token
for OAuthCredentials.
Args:
verifier: string, dict - either the verifier token, or a dictionary
of the query parameters to the callback, which contains
the oauth_verifier.
Returns:
The Credentials object.
"""
if not (isinstance(verifier, str) or isinstance(verifier, unicode)):
verifier = verifier['oauth_verifier']
token = oauth.Token(
self.request_token['oauth_token'],
self.request_token['oauth_token_secret'])
token.set_verifier(verifier)
consumer = oauth.Consumer(self.consumer_key, self.consumer_secret)
client = oauth.Client(consumer, token)
headers = {
'user-agent': self.user_agent,
'content-type': 'application/x-www-form-urlencoded'
}
uri = _oauth_uri('access', self.discovery, self.params)
resp, content = client.request(uri, 'POST', headers=headers)
if resp['status'] != '200':
logging.error('Failed to retrieve access token: %s', content)
raise RequestError('Invalid response %s.' % resp['status'])
oauth_params = dict(parse_qsl(content))
token = oauth.Token(
oauth_params['oauth_token'],
oauth_params['oauth_token_secret'])
return OAuthCredentials(consumer, token, self.user_agent)
示例13: __init__
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def __init__(self, consumer_key, consumer_secret, token_key, token_secret):
consumer = Consumer(key=consumer_key, secret=consumer_secret)
token = Token(key=token_key, secret=token_secret)
proxy_info = None
if hasattr(settings, 'PROXY_HOST') and \
hasattr(settings, 'PROXY_PORT'):
proxy_info = ProxyInfo(
proxy_type=PROXY_TYPE_HTTP,
proxy_host=settings.PROXY_HOST,
proxy_port=settings.PROXY_PORT)
self.client = Client(
consumer=consumer,
token=token,
proxy_info=proxy_info)
示例14: lrs_oauth_callback
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def lrs_oauth_callback(request):
import os
import urlparse
user_id = request.user.id
user = User.objects.get(id=user_id)
status = request.GET.get('status')
if status is not None and status == 'fail':
return HttpResponseServerError('Could not get access token.')
request_token = OAuthTempRequestToken.objects.get(user_id=user)
verifier = request.GET.get('oauth_verifier')
token = oauth.Token(request_token.token, request_token.secret)
request_token.delete() #delete temp token
token.set_verifier(verifier)
# Get Consumer info #Todo: change (most definitely) (IMPORTANT!!)
# consumer_key, consumer_secret = get_consumer_key_and_secret()
app = ClientApp.objects.get(id = request_token.clientapp.id)
client = oauth.Client(oauth.Consumer(app.get_key(), app.get_secret()), token)
# Exchange request_token for authed and verified access_token
resp,content = client.request(app.get_access_token_url(), "POST")
access_token = dict(urlparse.parse_qsl(content))
if access_token['oauth_token']:
UserAccessToken_LRS(user=user, access_token=access_token['oauth_token'],
access_token_secret=access_token['oauth_token_secret'],
clientapp = app).save()
from django.shortcuts import render_to_response
return render_to_response('xapi/get_access_token_successful.html')
示例15: to_string
# 需要导入模块: import oauth2 [as 别名]
# 或者: from oauth2 import Token [as 别名]
def to_string(self):
return oauth.Token(self.api_key, self.secret_key).to_string()