2

我正在尝试使用 OAuth 1.0a 授权我使用服务。我可以执行requestTokenauthorize步骤,但是当我调用accessTokenURL 时,我会收到401 Unauthorized响应并返回以下消息:
OAuth Verification Failed: Can't exchange request token "THE_TOKEN" for access token. No such token or not authorized%

我已经掩盖了凭据和 URL。

请求令牌 URL:https
://url-to-the-service.com/oauth/requestToken 授权令牌 URL:https
://url-to-the-service.com/oauth/authorize 访问令牌 URL:https:// url-to-the-service.com/oauth/accessToken

我从服务中获得的凭据如下:
consumer_key = CONSUMER_KEY
consumer_secret = CONSUMER_SECRET

第 1 步 - 请求临时令牌

curl -v -X GET --url "https://url-to-the-service.com/oauth/requestToken?oauth_version=1.0& \
oauth_timestamp=1516721112& \
oauth_nonce=25794& \
oauth_signature_method=PLAINTEXT& \
oauth_consumer_key=CONSUMER_KEY& \
oauth_signature=CONSUMER_SECRET%26"

然后服务响应:

oauth_callback_confirmed=true&oauth_token=THE_TOKEN&oauth_token_secret=THE_TOKEN_SECRET&xoauth_token_ttl=3600

第 2 步 - 使用临时令牌授权我并获得验证者

然后我在浏览器中输入:

https://url-to-the-service.com/oauth/authorize?oauth_token=THE_TOKEN

...它提示我登录服务。当我在登录后按下授权按钮时,我被转发到此 URL:

https://url-to-the-service.com/oauth/authorize?yes=1&oauthVerifier=123456789&oauth_token=THE_TOKEN

第 3 步 - 请求访问令牌

最后,我https://url-to-the-service.com/oauth/accessToken通过将oauth_verifier和令牌秘密添加到oauth_signature

curl -v -X GET --url "https://url-to-the-service.com/oauth/accessToken?oauth_version=1.0& \ 
oauth_timestamp=1516730938& \
oauth_nonce=30888& \
oauth_signature_method=PLAINTEXT& \
oauth_consumer_key=CONSUMER_KEY& \
oauth_signature=CONSUMER_SECRET%26THE_TOKEN_SECRET& \
oauth_token=THE_TOKEN& \
oauth_verifier=123456789"

但服务响应:
OAuth Verification Failed: Can't exchange request token "THE_TOKEN" for access token. No such token or not authorized%

那么我错过了什么?

4

2 回答 2

1

根据rfc5849 3.2 验证请求

当接收到带有无效客户端凭据无效或过期令牌无效签名无效或已使用 nonce的请求时,服务器应该返回 401(未授权)状态代码。

我不确定xoauth_token_ttl=3600步骤 1 的值是分钟还是秒。如果是秒,1516721112 的第一个请求和 1516730938 的访问令牌请求已经过期。(1516730938 - 1516721112 = 9826)

于 2018-02-02T13:36:37.127 回答
0

看起来您在 cURL 请求中强制执行 GET 请求。尝试如下 POST 请求:

curl -v -X POST --url "https://url-to-the-service.com/oauth/accessToken?
oauth_version=1.0& \ 
oauth_timestamp=1516730938& \
oauth_nonce=30888& \
oauth_signature_method=PLAINTEXT& \
oauth_consumer_key=CONSUMER_KEY& \
oauth_signature=CONSUMER_SECRET%26THE_TOKEN_SECRET& \
oauth_token=THE_TOKEN& \
oauth_verifier=123456789"

请参阅OAuth1 文档。在此引用以供参考:

代币兑换

授权的最后一步是将临时凭证(请求令牌)交换为长期凭证(也称为访问令牌)。此请求还会破坏临时凭证。

通过向令牌请求端点(通常是 /oauth1/access)发送 POST 请求,临时凭证将转换为长期凭证。此请求必须由临时凭证签名,并且必须包含来自授权步骤的 oauth_verifier 令牌。

于 2018-02-01T02:42:58.460 回答