我正在实施与Android的google buzz API集成。我已成功完成OAuth并获得了AccessToken。但是当我尝试在Buzz上发布消息时,它会回复我状态401并且响应下面的响应。
{ “错误”:{ “错误”:[{ “域”: “全局”, “理由”: “无效”, “消息”:“令牌 无效 - 无效的AuthSub 。令牌 “” 的locationType “:” 首部 “ ”位置“: ”授权“}], ”代码“:401, ”消息“:” 令牌 无效 - AuthSub令牌无效。“}}
我为oAuth使用了singpost库,下面是我用来向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."}}
是否有人可以帮助我。
感谢。