2012-07-28 41 views
5

向Blogger REST API(v3.0)發送DELETE請求,我試圖使用delete method刪除帖子。爲此,我使用下面的代碼:嘗試使用Blogger API刪除帖子會返回「未找到」錯誤

api_uri = 'https://www.googleapis.com/blogger/v3/blogs/%s/posts/%s' % (blogId, postId) 
result = urlfetch.fetch(url=api_uri, 
        method=urlfetch.DELETE, 
        headers={'Authorization' : oauth_token}) 

self.response.out.write(result.content) 

但服務器返回:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "notFound", 
    "message": "Not Found" 
    } 
    ], 
    "code": 404, 
    "message": "Not Found" 
} 
} 

不過,我可以檢索有關這篇文章的信息,使用下面的代碼:

api_uri = 'https://www.googleapis.com/blogger/v3/blogs/%s/posts/%s' % (blogId, postId) 
result = urlfetch.fetch(url=api_uri, 
        headers={'Authorization' : oauth_token}) 
self.response.out.write(result.content) 

在這一刻,我不明白我在做什麼錯了 - 請求被授權,blogIdpostId是正確的 - 但無論如何,服務器返回「不是發現「錯誤。

如果你知道如何解決這個問題,或者你可以給出有用的建議 - 請幫助我。
謝謝你的時間和對此事的考慮。


UPD 1:如果我將請求發送到以下網址:

# https://www.googleapis.com/blogger/v3/users/{userID} 
# https://www.googleapis.com/blogger/v3/users/self 

服務器也返回:

{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "notFound", 
    "message": "Not Found" 
    } 
    ], 
    "code": 404, 
    "message": "Not Found" 
} 
} 


UPD 2:我忘了說我正在使用OAuth 2.0 for Server to Server Applications

jwt_claim_set = { 
    'iss' : '{id}@developer.gserviceaccount.com', 
    'scope' : 'https://www.googleapis.com/auth/blogger', 
    'aud' : 'https://accounts.google.com/o/oauth2/token', 
    'exp' : expire, 
    'iat' : timestamp 
} 

服務器返回:因此,要獲得授權令牌,我使用下面的JWT聲明組發送請求https://accounts.google.com/o/oauth2/token

{ 
    "access_token" : "1/8xbJqaOZXSUZbHLl5EOtu1pxz3fmmetKx9W8CV4t79M", 
    "token_type" : "Bearer", 
    "expires_in" : 3600 
} 

,並定義變量oauth_token,使用:

data = simplejson.loads(result.content) 
oauth_token = data['token_type'] + ' ' + data['access_token'] 
+1

考慮到您可能已經檢查了ID和auth令牌,我建議您使用[cURL](http://en.wikipedia.org/wiki/CURL)手動測試您的查詢。根據結果​​,你會知道如果問題是你如何建立你的網址或其他東西... – 2012-07-31 13:11:26

+0

@AlexisHuet,謝謝你的建議,但無論如何,我敢肯定,問題不在URL中。當然,問題是「別的」。 – B7ackAnge7z 2012-07-31 14:07:29

+0

您是否用'requests'試過了,看看問題出在您使用的模塊中? – 2012-08-03 12:38:38

回答

4

您確定您正確使用OAuth2嗎?在我看來,你沒有正確登錄,這就是爲什麼你得到這些錯誤。

嘗試使用Google OAuh2 Playground(https://code.google.com/oauthplayground/)查看相同的查詢,看看會發生什麼。

+0

感謝您的信息,但正如我所說的,我正嘗試使用OAuth 2.0實現服務器到服務器應用程序。此時,OAuth 2.0 Playground不支持此授權方法。至於正確使用OAuth2,我認爲,如果出現錯誤,則會顯示另一個錯誤。 – B7ackAnge7z 2012-08-01 15:06:14

+0

爲什麼不嘗試在標頭中添加授權,只需在URL中添加?access_token = ?無論如何,這就是我用它來做的。 – pyriku 2012-08-01 15:32:12

+0

根據[文檔](https://developers.google.com/blogger/docs/3.0/using#DeletingAPost),有必要發送'Authorization'標頭。您是否意味着您正在使用'access_token = '將OAuth 2.0用於服務器到服務器應用程序? – B7ackAnge7z 2012-08-01 15:59:53

相關問題