2017-09-08 21 views
2

サーバーサイドのnode.jsを使用して、GmailのメッセージAPIにリクエストを送信しようとしています。送信DOESメールを値としてoauth2tokenと生のNode.js Gmail APIへのPOSTリクエストメッセージの送信

body: '{ 
"error": { 
    "errors": [ 
    { 
    "domain": "global", 
    "reason": "invalidArgument", 
    "message": "\'raw\' RFC822 payload 
    message string or uploading message via /upload/ URL required" 
    } 
    ], 
    "code": 400, 
    "message": "\'raw\' RFC822 payload message 
    string or uploading message via /upload/ URL required" 
} 
}' 
} 

入力パラメータに-実は、私はGoogleのOAuthの2遊び場(https://developers.google.com/oauthplayground)を有効に使用している場合やトークンを使用して生:私は次のエラーを取得しています。誰かが私が逃したものを見ることができますか?

function sendMail(oauth2token, raw) { 
    context.log('Token: ' + oauth2token); 
    context.log('raw: ' + raw); 

    var params = { 
     userId: user_id, 
     resource: { 'raw': raw} 
    }; 

    var headers = { 
     "HTTP-Version": "HTTP/1.1", 
     "Content-Type": "application/json", 
     "Authorization": "Bearer " + oauth2token 
    }; 

    var options = { 
     headers: headers, 
     url: "https://www.googleapis.com/gmail/v1/users/me/messages/send", 
     method: "POST", 
     params: params 
    }; 

    request(options, function (error, response, body) { 
     if (!error && response.statusCode == 200) { 
      context.log(body); 
     } 
     if (error) { 
      context.log(error); 
     } 
     else { 
      context.log(response); 
     } 
    }) 
} 
+0

私は同じ問題があります。あなたはこれを解決しましたか? – grabbag

答えて

0

Googleのプレイグラウンドでテストしていて、すべて見栄えが良ければ、使用している他の外部依存関係を見てみましょう。たとえば、要求。たぶんurlの代わりに解析されたurlオブジェクトを渡す必要があるかもしれません。これをチェックしてください:https://github.com/request/request#requestoptions-callback。あなたが唯一のbodyrawメッセージを渡す必要があり

Url { 
    protocol: 'https:', 
    slashes: true, 
    auth: null, 
    host: 'www.googleapis.com', 
    port: null, 
    hostname: 'www.googleapis.com', 
    hash: null, 
    search: null, 
    query: null, 
    pathname: '/gmail/v1/users/me/messages/send', 
    path: '/gmail/v1/users/me/messages/send', 
    href: 'https://www.googleapis.com/gmail/v1/users/me/messages/send' } 

こと、またはURL

0

するのではなく、URIに既存のオプションを変更します。ここでは

はあなた解析されたURLのオブジェクトは次のようになります。

function sendMail (oauth2token, raw) { 
    var options = { 
    method: 'POST', 
    url: 'https://www.googleapis.com/gmail/v1/users/me/messages/send', 
    headers: { 
     'HTTP-Version': 'HTTP/1.1', 
     'Content-Type': 'application/json', 
     'Authorization': 'Bearer ' + oauth2token 
    }, 
    body: JSON.stringify({ 
     raw: raw 
    }) 
    }; 

    request(options, function (error, response, body) { 
    if (!error && response.statusCode == 200) { 
     context.log(body); 
    } 
    if (error) { 
     context.log(error); 
    } else { 
     context.log(response); 
    } 
    }); 
} 
関連する問題