带路标的OAuts - 如何签署POST以获取访问令牌

时间:2009-11-20 19:01:31

标签: post oauth signpost auth-token

我与客户合作,他让我有点模糊 说明。这就是我正在做的事情(使用CommonsHttpOAuthConsumer作为 消费者和DefaultOAuthProvider作为提供者)

  1. 我可以通过这样做获得响应令牌:

    String requestToken = provider.retrieveRequestToken
    (OAuth.OUT_OF_BAND);
    

    这是带有params的URL形式,所以我正在解析实际的令牌 例如: https://foobar.com/oauth/login_authorize?oauth_token=XRFCGPbES3M2bYZy...

  2. 现在 - 我说的说明: Given the request token obtained in step 1, login with the user’s credentials (name and password) as POST parameters and sign the request with the request token/secret POST https://foobar.com/oauth/login_authorize

  3. 这就是我遇到困难的地方。显然我必须输入 requestToken在某处,所以我这样做(post是包含用户凭据的HttpPost):

    consumer.setTokenWithSecret(requestToken, SECRET);
    consumer.sign(post);
    

    它不起作用。它实际上产生了200个状态,但我得到的是一个 一般错误消息。

1 个答案:

答案 0 :(得分:0)

retrieveRequestToken不返回请求令牌,它会返回您需要发送给用户的authenticationUrl,以便他们可以登录。请求令牌保存在提供者对象中。

String authenticationUrl = provider.retrieveRequestToken( call_back_url )

注意:根据oauth标准,用户使用其凭据登录提供者站点。完成后,您(作为消费者)可以获得访问令牌,之后您可以访问提供商网站上的数据。

// After user has signed in
provider.retrieveAccessToken(null)

// access token is saved in provider and provider knows which consumer uses it
// so now you can sign with your consumer and connect with the request
URL url = new URL( protected_resources_url )
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request)
request.connect()

如果您拥有用户凭据,则可以在脚本中执行授权

// Using grails and functional-tests
get(authenticationUrl)
// Image the site shows a simple form with username/password and a login button
setRedirectEnabled false
form {
  username = "mario"
  password = "peach"

  click "login"
}

然后执行retrieveRequestToken和上面提到的代码

希望这会对你有所帮助 //乔纳斯