我正在实现 google Buzz API 与 android 的集成。我已经成功完成了 OAuth 并获得了 AccessToken。但是,当我尝试在 Buzz 上发布消息时,它会以状态 401 回复我,并回复如下。
{"error":{"errors":[{"domain":"global","reason":"invalid","message":"令牌无效 - AuthSub 令牌无效。","locationType":"header", "location":"Authorization"}],"code":401,"message":"令牌无效 - AuthSub 令牌无效。"}}
我将 singpost 库用于 oAuth,下面是我用来向 BUZZ 发布消息的代码。
String serurl = "https://www.googleapis.com/buzz/v1/activities/@me/@self?key=@@KEY&alt=json";
serurl = serurl.replace("@@KEY", AppSettings.GOOGLE_API_KEY);
CommonsHttpOAuthConsumer consumer = new CommonsHttpOAuthConsumer(
AppSettings.GOOGLE_CONSUMER_KEY,
AppSettings.GOOGLE_CONSUMER_SECRET);
consumer.setMessageSigner(new HmacSha1MessageSigner());
consumer.setTokenWithSecret(destination.AccessToken,
destination.AccessTokenSecret);
String bstr = "{\"data\": {\"object\": {\"type\": \"note\",\"content\": \""
+ msg + "\"}}}";
HttpClient client = new DefaultHttpClient();
HttpPost method = new HttpPost(consumer.sign(serurl));
method.setHeader("Content-Type", "application/json");
method.addHeader("Accept", "application/json");
method.addHeader("Authorization", URLEncoder.encode(destination.AccessToken));
method.setEntity(new StringEntity(bstr, "UTF-8"));
HttpResponse response = client.execute(method);
final HttpEntity entity = response.getEntity();
String responseString = "";
if (entity != null) {
InputStream inputStream = null;
try {
inputStream = entity.getContent();
responseString = Utils.convertinputStreamToString(new Utils.FlushedInputStream(
inputStream));
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
}
System.out.println(responseString);
我得到了这个回应:
{"error":{"errors":[{"domain":"global","reason":"invalid","message":"Token invalid - Invalid AuthSub token.","locationType":"header","location":"Authorization"}],"code":401,"message":"Token invalid - Invalid AuthSub token."}}
有没有人可以在这方面帮助我。
谢谢。