2012-08-14 137 views
1

我想使用Blogger API將帖子添加到我的博客。我成功獲得使用Blogger API的權利,並在Google API控制檯中將其激活。我用this教程來獲取access_token。我發現this question,所以在請求之前,我獲得了新的request_token。Blogger JSON API添加帖子

當我第一次請求添加帖子時,出現en錯誤:401「消息」:「無效憑證」,「位置」:「授權」

當我做添加後與新的令牌第二個請求,我得到了錯誤:403「消息」:「每日超限請註冊」

代碼爲我的要求是:

final JSONObject obj = new JSONObject(); 
obj.put("id", mUserID); 

final JSONObject requestBody = new JSONObject(); 
requestBody.put("kind", "blogger#post"); 
requestBody.put("blog", obj); 
requestBody.put("title", msg[0]); 
requestBody.put("content", msg[0] + " " + msg[1]); 

final HttpPost request = new HttpPost("https://www.googleapis.com/blogger/v3/blogs/" + mUserID + "/posts"); 
request.addHeader("Authorization", "Bearer " + mToken); 
request.addHeader("Content-Type", "application/json"); 
request.setEntity(new StringEntity(requestBody.toString())); 
final HttpResponse response = mHttpClient.execute(request); 

final HttpEntity ent = response.getEntity(); 
Log.i(SocialPoster.LOG, EntityUtils.toString(ent)); 
ent.consumeContent(); 

UPDATE 解決方案發現:「關鍵= {} MY_API_KEY」簡單地添加到請求的URL解決了這個問題

回答

2

教程的網站,你李nked狀態

"The API Key is mandatory as it identifies your application and therefore allows the API to deduct quota and use the quota rules defined for your project. You need to specify the API Key on your Tasks service Object."

useTasksAPI(String accessToken) { 
    // Setting up the Tasks API Service 
    HttpTransport transport = AndroidHttp.newCompatibleTransport(); 
    AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken); 
    Tasks service = new Tasks(transport, accessProtectedResource, new JacksonFactory()); 
    service.accessKey = INSERT_YOUR_API_KEY; 
    service.setApplicationName("Google-TasksSample/1.0"); 

    // TODO: now use the service to query the Tasks API 
} 

聽起來像你對我缺少API密鑰,使用它錯了,放錯了地方在你的代碼,或者在錯誤的方式提供給服務。

我沒有看過這裏的代碼,但這是谷歌的示例代碼,你正在嘗試做什麼。測試您的API密鑰與this code.

+0

謝謝你的答案,我試圖添加「?key = 」請求的網址,它的工作原理=) – 2012-08-14 09:02:02

+0

真棒,很高興在服務:) – furier 2012-08-14 09:06:00

相關問題