0

私の会社のドメインの下にあるすべてのメールを取得しようとしています。私はNode.jsクライアントライブラリを使用しています。最後のapproachでは、クイックスタートガイドに記載されているようにOauthメソッドを使用しました。 JWTを使用して承認する必要があることが示唆されましたが、ノードで適切に行う方法(ドキュメントが不足しているため)がまだわかりません。Gmail API - google.auth.jwtを使用してドメイン内のユーザーからメッセージを取得できません

{ Error: Bad Request 
    at Request._callback (/home/kunok/code/gapi/node_modules/google-auth-library/lib/transporters.js:85:15) 
    at Request.self.callback (/home/kunok/code/gapi/node_modules/google-auth-library/node_modules/request/request.js:198:22) 
    at emitTwo (events.js:106:13) 
    at Request.emit (events.js:191:7) 
    at Request.<anonymous> (/home/kunok/code/gapi/node_modules/google-auth-library/node_modules/request/request.js:1057:14) 
    at emitOne (events.js:101:20) 
    at Request.emit (events.js:188:7) 
    at IncomingMessage.<anonymous> (/home/kunok/code/gapi/node_modules/google-auth-library/node_modules/request/request.js:1003:12) 
    at emitNone (events.js:91:20) 
    at IncomingMessage.emit (events.js:185:7) 
    code: 400, 
    errors: 
    [ { domain: 'global', 
     reason: 'failedPrecondition', 
     message: 'Bad Request' } ] } 

私は右のアプローチを理解することができません:私はエラーを取得する

var google = require('googleapis'), 
    gmail = google.gmail('v1'); 
var key = require('./service_key.json'); 

var jwtClient = new google.auth.JWT(
    key.client_email, 
    null, 
    key.private_key, 
    ['https://mail.google.com/','https://www.googleapis.com/auth/admin.directory.group'] 
); 

jwtClient.authorize(function(err, tokens) { 
    if (err) { 
    console.log(err); 
    return; 
    } 

    gmail.users.messages.list({userId: 'me', auth: jwtClient}, (err, response)=>{ 
     if (err) { 
     console.log(err); 
     return; 
     } 
     console.log(response); 
    });//messages.list 

});//jwtClient.authorize 

:これは私のコードです。サーバーからCRONジョブnode.jsスクリプトを実行することで、ユーザー対話をスキップしてドメイン下のすべてのメールをフェッチしたいと考えています。私はすべての管理者権限を持っています。私のアプローチで何が間違っていますか?

"""Build and returns an Admin SDK Directory service object authorized with the service accounts 
that act on behalf of the given user. 

Args: 
user_email: The email of the user. Needs permissions to access the Admin APIs. 
Returns: 
Admin SDK directory service object. 
""" 

credentials = ServiceAccountCredentials.from_p12_keyfile(
SERVICE_ACCOUNT_EMAIL, 
SERVICE_ACCOUNT_PKCS12_FILE_PATH, 
'notasecret', 
scopes=['https://www.googleapis.com/auth/admin.directory.user']) 

credentials = credentials.create_delegated(user_email) 

・ホープ、このことができます:

答えて

関連する問題