本文整理汇总了Python中oauthlib.oauth1.rfc5849.Client类的典型用法代码示例。如果您正苦于以下问题:Python Client类的具体用法?Python Client怎么用?Python Client使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Client类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_request_token
def get_request_token(public_key, secret_key, url, method=u'POST'):
'''
:param public_key:
:param secret_key:
:param url:
:param method:
'''
c = Client(
unicode(public_key),
unicode(secret_key),
signature_type=SIGNATURE_TYPE_QUERY
)
uri, headers, body = c.sign(
uri=url,
http_method=method)
http = httplib2.Http()
response, content = httplib2.Http.request(http, uri,
method=method,
body=body,
headers=headers
)
if response.get('status') != '200':
raise Exception("Invalid request token response: %s." % content)
tokens = dict(urlparse.parse_qsl(content))
token = tokens.get('oauth_token')
token_secret = tokens.get('oauth_token_secret')
return unicode(token), unicode(token_secret)
示例2: test_uri_provided_realm
def test_uri_provided_realm(self):
client = Client("foo", callback_uri="https://c.b/cb", client_secret="bar")
uri = self.uri + "?realm=foo"
_, headers, _ = client.sign(uri)
u, h, b, s = self.endpoint.create_request_token_response(uri, headers=headers)
self.assertEqual(s, 200)
self.assertIn("oauth_token", b)
示例3: test_rsa_method
def test_rsa_method(self):
private_key = (
"-----BEGIN RSA PRIVATE KEY-----\nMIICXgIBAAKBgQDk1/bxy"
"S8Q8jiheHeYYp/4rEKJopeQRRKKpZI4s5i+UPwVpupG\nAlwXWfzXw"
"SMaKPAoKJNdu7tqKRniqst5uoHXw98gj0x7zamu0Ck1LtQ4c7pFMVa"
"h\n5IYGhBi2E9ycNS329W27nJPWNCbESTu7snVlG8V8mfvGGg3xNjT"
"MO7IdrwIDAQAB\nAoGBAOQ2KuH8S5+OrsL4K+wfjoCi6MfxCUyqVU9"
"GxocdM1m30WyWRFMEz2nKJ8fR\np3vTD4w8yplTOhcoXdQZl0kRoaD"
"zrcYkm2VvJtQRrX7dKFT8dR8D/Tr7dNQLOXfC\nDY6xveQczE7qt7V"
"k7lp4FqmxBsaaEuokt78pOOjywZoInjZhAkEA9wz3zoZNT0/i\nrf6"
"qv2qTIeieUB035N3dyw6f1BGSWYaXSuerDCD/J1qZbAPKKhyHZbVaw"
"Ft3UMhe\n542UftBaxQJBAO0iJy1I8GQjGnS7B3yvyH3CcLYGy296+"
"XO/2xKp/d/ty1OIeovx\nC60pLNwuFNF3z9d2GVQAdoQ89hUkOtjZL"
"eMCQQD0JO6oPHUeUjYT+T7ImAv7UKVT\nSuy30sKjLzqoGw1kR+wv7"
"C5PeDRvscs4wa4CW9s6mjSrMDkDrmCLuJDtmf55AkEA\nkmaMg2PNr"
"jUR51F0zOEFycaaqXbGcFwe1/xx9zLmHzMDXd4bsnwt9kk+fe0hQzV"
"S\nJzatanQit3+feev1PN3QewJAWv4RZeavEUhKv+kLe95Yd0su7lT"
"LVduVgh4v5yLT\nGa6FHdjGPcfajt+nrpB1n8UQBEH9ZxniokR/IPv"
"dMlxqXA==\n-----END RSA PRIVATE KEY-----"
)
client = Client('client_key', signature_method=SIGNATURE_RSA,
rsa_key=private_key, timestamp='1234567890', nonce='abc')
u, h, b = client.sign('http://example.com')
correct = ('OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
'oauth_version="1.0", oauth_signature_method="RSA-SHA1", '
'oauth_consumer_key="client_key", '
'oauth_signature="ktvzkUhtrIawBcq21DRJrAyysTc3E1Zq5GdGu8EzH'
'OtbeaCmOBDLGHAcqlm92mj7xp5E1Z6i2vbExPimYAJL7FzkLnkRE5YEJR4'
'rNtIgAf1OZbYsIUmmBO%2BCLuStuu5Lg3tAluwC7XkkgoXCBaRKT1mUXzP'
'HJILzZ8iFOvS6w5E%3D"')
self.assertEqual(h['Authorization'], correct)
示例4: get_access_token
def get_access_token(request_token, request_token_secret,
access_token_url, access_token_method,
public_key, secret_key,
verifier):
"""
Returns access token and access token secret
"""
c = Client(public_key,
secret_key,
resource_owner_key=request_token,
resource_owner_secret=request_token_secret,
signature_type=SIGNATURE_TYPE_QUERY,
verifier=verifier
)
uri, headers, body = c.sign(uri=access_token_url, http_method=access_token_method)
http = httplib2.Http()
response, content = httplib2.Http.request(http, uri, method=access_token_method, body=body,
headers=headers)
if response.get('status') != '200':
raise Exception("Invalid access token response: %s." % content)
tokens = dict(urlparse.parse_qsl(content))
access_token = tokens.get('oauth_token')
access_token_secret = tokens.get('oauth_token_secret')
return access_token, access_token_secret
示例5: test_check_redirect_uri
def test_check_redirect_uri(self):
client = Client('foo')
uri, headers, _ = client.sign(self.uri)
h, b, s = self.endpoint.create_request_token_response(
uri, headers=headers)
self.assertEqual(s, 400)
self.assertIn('invalid_request', b)
示例6: test_client_realm_sign_with_additional_realm
def test_client_realm_sign_with_additional_realm(self):
client = Client("client-key", realm="moo-realm")
uri, header, body = client.sign("http://example-uri", realm="baa-realm")
self.assertTrue(
header["Authorization"].startswith('OAuth realm="baa-realm",'))
# make sure sign() does not override the default realm
self.assertEqual(client.realm, "moo-realm")
示例7: test_can_load_consumer_secret_from_settings
def test_can_load_consumer_secret_from_settings(self):
lti = Client(
client_key=OTHER_LTI_CONSUMER_KEY,
client_secret=OTHER_LTI_CONSUMER_SECRET,
signature_type=SIGNATURE_TYPE_BODY,
)
(uri, _headers, body) = lti.sign(
uri=self.url_prefix + LTI_TPA_LOGIN_URL, http_method='POST',
headers={'Content-Type': FORM_ENCODED},
body={
'user_id': LTI_USER_ID,
'custom_tpa_next': '/account/finish_auth/?course_id=my_course_id&enrollment_action=enroll',
}
)
with self.settings(SOCIAL_AUTH_LTI_CONSUMER_SECRETS={OTHER_LTI_CONSUMER_KEY: OTHER_LTI_CONSUMER_SECRET}):
login_response = self.client.post(path=uri, content_type=FORM_ENCODED, data=body)
# The user should be redirected to the registration form
self.assertEqual(login_response.status_code, 302)
self.assertTrue(login_response['Location'].endswith(reverse('signin_user')))
register_response = self.client.get(login_response['Location'])
self.assertEqual(register_response.status_code, 200)
self.assertIn(
'"currentProvider": "Tool Consumer with Secret in Settings"',
register_response.content
)
self.assertIn('"errorMessage": null', register_response.content)
示例8: test_hmac_sha1_method
def test_hmac_sha1_method(self):
client = Client('client_key', timestamp='1234567890', nonce='abc')
u, h, b = client.sign('http://example.com')
correct = ('OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
'oauth_version="1.0", oauth_signature_method="HMAC-SHA1", '
'oauth_consumer_key="client_key", '
'oauth_signature="hH5BWYVqo7QI4EmPBUUe9owRUUQ%3D"')
self.assertEqual(h['Authorization'], correct)
示例9: test_validate_signature
def test_validate_signature(self):
client = Client('foo',
resource_owner_key='token',
resource_owner_secret='secret')
_, headers, _ = client.sign(self.uri + '/extra')
v, r = self.endpoint.validate_protected_resource_request(
self.uri, headers=headers)
self.assertFalse(v)
示例10: test_decoding
def test_decoding(self):
client = Client('client_key', decoding='utf-8')
uri, headers, body = client.sign('http://a.b/path?query', body='a=b',
headers={'Content-Type': 'application/x-www-form-urlencoded'})
self.assertIsInstance(uri, bytes_type)
self.assertIsInstance(body, bytes_type)
for k, v in headers.items():
self.assertIsInstance(k, bytes_type)
self.assertIsInstance(v, bytes_type)
示例11: test_sign_body
def test_sign_body(self):
client = Client("client_key")
_, h, b = client.sign(
"http://i.b/path",
http_method="POST",
body="",
headers={"Content-Type": "application/x-www-form-urlencoded"},
)
self.assertEqual(h["Content-Type"], "application/x-www-form-urlencoded")
示例12: test_hmac_sha256_method
def test_hmac_sha256_method(self):
client = Client('client_key', signature_method=SIGNATURE_HMAC_SHA256,
timestamp='1234567890', nonce='abc')
u, h, b = client.sign('http://example.com')
correct = ('OAuth oauth_nonce="abc", oauth_timestamp="1234567890", '
'oauth_version="1.0", oauth_signature_method="HMAC-SHA256", '
'oauth_consumer_key="client_key", '
'oauth_signature="JzgJWBxX664OiMW3WE4MEjtYwOjI%2FpaUWHqtdHe68Es%3D"')
self.assertEqual(h['Authorization'], correct)
示例13: test_validate_signature
def test_validate_signature(self):
client = Client('foo',
resource_owner_key='token',
resource_owner_secret='secret',
verifier='verfier')
_, headers, _ = client.sign(self.uri + '/extra')
h, b, s = self.endpoint.create_access_token_response(
self.uri, headers=headers)
self.assertEqual(s, 401)
示例14: test_uri_provided_realm
def test_uri_provided_realm(self):
client = Client('foo', callback_uri='https://c.b/cb',
client_secret='bar')
uri = self.uri + '?realm=foo'
_, headers, _ = client.sign(uri)
h, b, s = self.endpoint.create_request_token_response(
uri, headers=headers)
self.assertEqual(s, 200)
self.assertIn('oauth_token', b)
示例15: test_params_in_query
def test_params_in_query(self):
client = Client('client_key', signature_type=SIGNATURE_TYPE_QUERY,
timestamp='1378988215', nonce='14205877133089081931378988215')
u, _, _ = client.sign('http://i.b/path', http_method='POST')
correct = ('http://i.b/path?oauth_nonce=14205877133089081931378988215&'
'oauth_timestamp=1378988215&'
'oauth_version=1.0&'
'oauth_signature_method=HMAC-SHA1&'
'oauth_consumer_key=client_key&'
'oauth_signature=08G5Snvw%2BgDAzBF%2BCmT5KqlrPKo%3D')
self.assertEqual(u, correct)