我在抄写员页面上尝试了这个例子,但有一个回调。我总是收到错误“无法使用 OAuth 进行身份验证”。我尝试使用返回的访问令牌的那一刻。
service = new ServiceBuilder()
.provider(TwitterApi.class)
.apiKey(config.getTwitterKey())
.apiSecret(config.getTwitterSecret())
.callback(config.getServerPath()+"/oauth/twitter")
.build();
Token requestToken = service.getRequestToken();
service.getAuthorizationUrl(requestToken)
在回调中,我尝试了两种不同的方法,使用第一步中的请求令牌或 new Token(tokenParam,verifier) 但没有区别,两者都返回了一个不起作用的访问令牌。
String tokenParam = request.getParameter(TOKEN);
String verifier = request.getParameter(VERIFIER);
Token accessToken = service.getAccessToken(requestToken,new Verifier(verifier));
带有访问令牌的响应流包含用户 ID 和显示名称,所以在这一步之前一切正常。
OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1/account/verify_credentials.json");
service.signRequest(accessToken, request); // the access token from step 4
request.send().getBody();
我尝试使用 xml url 和 json url,但我在 getBody() 方法中不断收到错误消息。{"error":"Could not authenticate with OAuth.","request":"\/1\/account\/verify_credentials.json"}
编辑:抄写调试输出:获取访问令牌
15:01:08,136 INFO [STDOUT] obtaining request token from https://api.twitter.com/oauth/request_token
15:01:08,141 INFO [STDOUT] setting oauth_callback to http://127.0.0.1:8080/myapp/oauth/twitter
15:01:08,145 INFO [STDOUT] generating signature...
15:01:08,380 INFO [STDOUT] base string is: POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Frequest_token&oauth_callback%3Dhttp%253A%252F%252F127.0.0.1%253A8080%252Fmyapp%252Foauth%252Ftwitter%26oauth_consumer_key%3DXXXXXXXXXX%2520%26oauth_nonce%3D583776017%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1346763668%26oauth_version%3D1.0
15:01:08,380 INFO [STDOUT] signature is: FagkFuKWHMRfoAQFS4tmMMO9FvI=
15:01:08,381 INFO [STDOUT] appended additional OAuth parameters: { oauth_callback -> http://127.0.0.1:8080/myapp/oauth/twitter , oauth_signature -> FagkFuKWHMRfoAQFS4tmMMO9FvI= , oauth_version -> 1.0 , oauth_nonce -> 583776017 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> XXXXXXXXXX , oauth_timestamp -> 1346763668 }
15:01:08,382 INFO [STDOUT] using Http Header signature
15:01:08,383 INFO [STDOUT] sending request...
15:01:09,564 INFO [STDOUT] response status code: 200
15:01:09,564 INFO [STDOUT] response body: oauth_token=dr7cT1xxxxK777Op7fjXeDMCqlY2DdAyyECLnVbhqLI&oauth_token_secret=xxxxxc7qB6pAzjnDrk3fDirakPVKxlS9c&oauth_callback_confirmed=true
发送签名请求
15:01:14,851 INFO [STDOUT] obtaining access token from https://api.twitter.com/oauth/access_token
15:01:14,852 INFO [STDOUT] setting token to: Token[dr7cTxxxxxXeDMCqlY2DdAyyECLnVbhqLI , 9ME2xglzfa0xxxxrk3fDirakPVKxlS9c] and verifier to: org.scribe.model.Verifier@96cb5a
15:01:14,852 INFO [STDOUT] generating signature...
15:01:14,853 INFO [STDOUT] base string is: POST&https%3A%2F%2Fapi.twitter.com%2Foauth%2Faccess_token&oauth_consumer_key%3DXXXXXXXXXXXX%2520%26oauth_nonce%3D2130885611%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1346763674%26oauth_token%3Ddr7cTxxxxxXeDMCqlY2DdAyyECLnVbhqLI%26oauth_verifier%3DrbRauznYtglz0eKDtCz7B2yeM%26oauth_version%3D1.0
15:01:14,853 INFO [STDOUT] signature is: OPJPKxP6veY/U/Fc9ZQU4a7vkaQ=
15:01:14,854 INFO [STDOUT] appended additional OAuth parameters: { oauth_signature -> OPJPKxP6veY/U/Fc9ZQU4a7vkaQ= , oauth_version -> 1.0 , oauth_nonce -> 2130885611 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> XXXXXXXXXXXXXXX , oauth_token -> dr7cT1SLFaK77DdAyyECLnVbhqLI , oauth_verifier -> rbRauznYtgugBLzgQRlz0eKDtCz7B2yeM , oauth_timestamp -> 1346763674 }
15:01:14,854 INFO [STDOUT] using Http Header signature
15:01:15,792 INFO [STDOUT] signing request: https://api.twitter.com/1/account/verify_credentials.json
15:01:15,792 INFO [STDOUT] setting token to: Token[15751501-mQZHUAHIzPrYUjwfD4IWvQunC3tTKdUIT4AmdjNvQ , Iw0pNVkhB5Ld4J36sZtiEmawH8XHR9hgvBNgAseTj0]
15:01:15,792 INFO [STDOUT] generating signature...
15:01:15,796 INFO [STDOUT] base string is: GET&https%3A%2F%2Fapi.twitter.com%2F1%2Faccount%2Fverify_credentials.json&oauth_consumer_key%3DXXXXXXXXXXXXXXXXX%2520%26oauth_nonce%3D139442186%26oauth_signature_method%3DHMAC-SHA1%26oauth_timestamp%3D1346763675%26oauth_token%3D15751501-mQZHUAHIzPrYUjwfD4IWvQunC3tTKdUIT4AmdjNvQ%26oauth_version%3D1.0
15:01:15,796 INFO [STDOUT] signature is: SziWPSPcEKRq0UE2AvkGvf8rIuo=
15:01:15,797 INFO [STDOUT] appended additional OAuth parameters: { oauth_signature -> SziWPSPcEKRq0UE2AvkGvf8rIuo= , oauth_version -> 1.0 , oauth_nonce -> 139442186 , oauth_signature_method -> HMAC-SHA1 , oauth_consumer_key -> XXXXXXXXXXXXXXXXXX , oauth_token -> 15751501-mQZHUAHIzPrYUjwfD4IWvQunC3tTKdUIT4AmdjNvQ , oauth_timestamp -> 1346763675 }
15:01:15,797 INFO [STDOUT] using Http Header signature
15:01:17,801 INFO [STDOUT] {"error":"Could not authenticate with OAuth.","request":"\/1\/account\/verify_credentials.json"}