2016-02-03 68 views
3

我正在使用google的API客戶端與Gmail API進行交互。假設我有一個草稿的不可變ID,我想發送相關的草稿。使用googleapiclient通過Id發送電子郵件草稿

我想:

service.users().drafts().send(
    userId='me', id=draft_id).execute(http=http) 

這裏draft_id的是,我想送草案的ID,httpHttp一個實例,對於其他請求的作品(所以它正確地驗證)。

嘗試上述,我得到一個TypeError

Traceback (most recent call last): 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/Users/mgilson/git/spider-web/app/pattern/handlers/ajax/email.py", line 77, in post 
    'success': int(client.send_draft(draft_id)) 
    File "/Users/mgilson/git/spider-web/app/pattern/services/email/gmail.py", line 601, in send_draft 
    userId='me', id=draft_id) \ 
    File "/Users/mgilson/git/spider-web/app/third_party/googleapiclient/discovery.py", line 669, in method 
    raise TypeError('Got an unexpected keyword argument "%s"' % name) 
TypeError: Got an unexpected keyword argument "id" 

的文檔有一個Java example,但沒有蟒蛇的例子。

service.users().drafts().send(
    userId='me', draftId=draft_id).execute(http=http) 

service.users().drafts().send(
    userId='me', draft_id=draft_id).execute(http=http) 

service.users().drafts().send(
    userId='me', body={'draft': {'id': draft_id}}).execute(http=http) 

最後一個實際上給出了一個不同的錯誤:

Traceback (most recent call last): 
    File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch 
    return method(*args, **kwargs) 
    File "/Users/mgilson/git/spider-web/app/pattern/handlers/ajax/email.py", line 77, in post 
    'success': int(client.send_draft(draft_id)) 
    File "/Users/mgilson/git/spider-web/app/pattern/services/email/gmail.py", line 603, in send_draft 
    .execute(http=self._http) 
    File "/Users/mgilson/git/spider-web/app/third_party/oauth2client/util.py", line 140, in positional_wrapper 
    return wrapped(*args, **kwargs) 
    File "/Users/mgilson/git/spider-web/app/third_party/googleapiclient/http.py", line 729, in execute 
    raise HttpError(resp, content, uri=self.uri) 
HttpError: <HttpError 400 when requesting https://www.googleapis.com/gmail/v1/users/me/drafts/send?alt=json returned "Invalid draft"> 

這讓我覺得它可能在正確的方向移動

,我試過其他變化...(注意,我已經能夠使用ID從上面鏈接的API瀏覽器發送消息,所以我相信我正在使用有效的ID)

什麼是正確的方式發送這些數據?


我不知道有關這是怎樣,但Gmail的API發現API的表示可以在https://www.googleapis.com/discovery/v1/apis/gmail/v1/rest

具體可以找到,我認爲我與被定義工作方法以下JSON:

"send": { 
    "id": "gmail.users.drafts.send", 
    "path": "{userId}/drafts/send", 
    "httpMethod": "POST", 
    "description": "Sends the specified, existing draft to the recipients in the To, Cc, and Bcc headers.", 
    "parameters": { 
    "userId": { 
    "type": "string", 
    "description": "The user's email address. The special value me can be used to indicate the authenticated user.", 
    "default": "me", 
    "required": true, 
    "location": "path" 
    } 
    }, 
    "parameterOrder": [ 
    "userId" 
    ], 
    "request": { 
    "$ref": "Draft" 
    }, 
    "response": { 
    "$ref": "Message" 
    }, 
    "scopes": [ 
    "https://mail.google.com/", 
    "https://www.googleapis.com/auth/gmail.compose", 
    "https://www.googleapis.com/auth/gmail.modify" 
    ], 
    "supportsMediaUpload": true, 
    "mediaUpload": { 
    "accept": [ 
    "message/rfc822" 
    ], 
    "maxSize": "35MB", 
    "protocols": { 
    "simple": { 
     "multipart": true, 
     "path": "/upload/gmail/v1/users/{userId}/drafts/send" 
    }, 
    "resumable": { 
     "multipart": true, 
     "path": "/resumable/upload/gmail/v1/users/{userId}/drafts/send" 
    } 
    } 
    } 
    }, 
+0

你試過'draftId = draft_id'? –

+0

@PauloScardine - 是的,我已經給出了一個鏡頭,沒有運氣。 – mgilson

回答

2

望着Gmail API Python docs,它看起來像第二個參數是請求的body。按照Gmail API reference,你需要一個id場在身體和你要發送的草案draftId

service.users().drafts().send(
    userId='me', body={ 'id': draft_id }).execute(http=http) 
+1

你已經接近了(儘管如此,我認爲我會得到一個'SyntaxError',因爲位置參數不能來_after_關鍵字參數,並且不允許任何位置參數)。它應該是'service.users()。drafts()。send(userId ='me',body = {'id':draft_id})。execute(http = http)'。如果你更新,我會在我有機會的時候接受。 – mgilson

+0

@mgilson啊,你是對的。我有限的Python技能讓我失望。 :)更新了答案。 – Tholle