0

Google은 회사 도메인에서 모든 이메일을 가져 오려고합니다. 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 스크립트를 실행하여 사용자 상호 작용을 건너 뛰고 도메인 아래의 모든 메일을 가져 오려고합니다. 나는 모든 권리를 가지고있다. 내 접근 방식에서 잘못된 점은 무엇입니까?

답변

0

시도는 Perform Google Apps Domain-Wide Delegation of Authority 코드 샘플 (파이썬)에서 jwt format

var jwt = new googleapis.auth.JWT(
SERVICE_ACCOUNT_EMAIL, 
SERVICE_ACCOUNT_KEY_FILE, 
null, 
['https://www.googleapis.com/auth/admin.directory.group'], 
[email protected] 
); 

을 기반으로 변경 :

"""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) 

희망이 도움이!

관련 문제