[英]Using OAuth2 with username and password
I have gained REST API acess to a certain service as part of a beta. 作为测试版的一部分,我已获得REST API访问特定服务。 I was told the authorization is throught OAuth2. 我被告知授权是通过OAuth2进行的。
I got the following: 我得到以下内容:
I also got a code sample in Ruby: 我还在Ruby中获得了一个代码示例:
client = OAuth2::Client.new(key, secret, :site => site)
token = client.password.get_token('your_email@mail.com', 'your_password')
access_token = OAuth2::AccessToken.new(client, token)
JSON.parse access_token.get("/v1/users/me").body rescue {}
I'm trying to implement this same snippet in python with the oauth2
package, without success: 我正在尝试使用oauth2
包在python中实现这个相同的代码段,但没有成功:
consumer = oauth2.Consumer(key=self._client_id,
secret=self._client_secret)
request_token_url = "api.theservice.com/"
token = oauth2.Token(key=self._email, secret=self._password)
client = oauth2.Client(consumer, token)
resp, content = client.request(request_token_url, "GET")
pprint.pprint(resp)
pprint.pprint(content)
resp, content = client.request(request_token_url + 'v1/users/me', "GET")
pprint.pprint(resp)
pprint.pprint(content)
The second response contains the following: 第二个响应包含以下内容:
'www-authenticate': 'Bearer realm="Doorkeeper", error="invalid_token", '
'error_description="The access token is invalid"',
I also tried creating a oauth2.Client
object without a token, and checked the first response for an access_token
, but nothing of the sort came through. 我还尝试在没有令牌的情况下创建一个oauth2.Client
对象,并检查了access_token
的第一个响应,但没有任何类型的响应。
What is the proper way to authenticate here? 在这里进行身份验证的正确方法是什么?
As far as I see neither oauth2 nor requests-oauth2 support username/password strategy. 据我所知,oauth2和requests-oauth2都不支持用户名/密码策略。 So you need to obtain access token by yourself. 所以你需要自己获取访问令牌。
Check your API docs for details, but in general it should be something like that: 检查您的API文档以获取详细信息,但一般情况下应该是这样的:
r = requests.post('http://api.theservice.com/auth',
data = {'email':email, 'password': password}).json()
token = oauth2.Token(key=r['key'], secret=r['secret'])
consumer = oauth2.Consumer(key=self._client_id,
secret=self._client_secret)
client = oauth2.Client(consumer, token)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.