Twitter API状态更新始终返回“不正确的签名”

时间:2010-10-05 21:48:53

标签: java google-app-engine twitter oauth signpost

我正在使用Signpost作为OAuth实现发布到Twitter。并且实现了GoogleAppEngineOAuthConsumerGoogleAppEngineOAuthProvider类,但由于它们非常简单,所以我不在这里提供它们的来源。

这是我的身份验证部分,似乎工作正常。

LoginServlet.java:

// fetching the request token
OAuthConsumer consumer = new GoogleAppEngineOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
OAuthProvider provider = new GoogleAppEngineOAuthProvider(REQUEST_TOKEN_URL, ACCESS_TOKEN_URL, AUTHORIZATION_URL);
String redirectUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL);
// cache the request token and request token secret
response.sendRedirect(redirectUrl);

CallbackServlet.java

// fetching the access token
String verifier = (String) req.getParameter("oauth_verifier");
// retrieve request token and request token secret from cache
OAuthConsumer consumer = new GoogleAppEngineOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
OAuthProvider provider = new GoogleAppEngineOAuthProvider(REQUEST_TOKEN_URL, 
consumer.setTokenWithSecret(token, tokenSecret);
provider.setOAuth10a(true);
provider.retrieveAccessToken(consumer, verifier);
// store access token and access token secret

这是实际存在问题的部分。

TweetServlet.java

OAuthConsumer consumer = new GoogleAppEngineOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
// retrieve access token and access token secret from storage
consumer.setTokenWithSecret(accessToken, accessTokenSecret);
final HTTPRequest updateStatus = new HTTPRequest(new URL("http://api.twitter.com/1/statuses/update.json"), HTTPMethod.POST);
updateStatus.setPayload(("status=" + URLEncoder.encode(message, "UTF-8")).getBytes());
consumer.sign(updateStatus);
logger.debug(new String(URLFetchServiceFactory.getURLFetchService().fetch(updateStatus).getContent()));

每次结果:{"request":"/1/statuses/update.json","error":"Incorrect signature"}

1 个答案:

答案 0 :(得分:2)

我能够自己解决这个问题。问题是我没有为请求设置Content-Type标头,因此签名没有签署参数,导致无效签名。一旦我将其设置为application/x-www-form-urlencoded,它就开始工作了。

final HTTPRequest updateStatus = new HTTPRequest(new URL("http://api.twitter.com/1/statuses/update.json"), HTTPMethod.POST);
updateStatus.addHeader(new HTTPHeader("Content-Type", "application/x-www-form-urlencoded"));