Gmail API domain-wide delegation

前端 未结 2 1000
北荒
北荒 2020-12-22 01:21

I am using Gmail API and I am trying to fetch emails from all users under company. But when I run code such as:

function runAPI(auth) {
  var gmail = google.         


        
2条回答
  •  南方客
    南方客 (楼主)
    2020-12-22 01:32

    The setup is fine, however in your code you're doing it wrong.

    This code:

    gmail.users.threads.list({auth: auth, userId: '108800016305523828361'},'',function(err, response)

    specifically.

    userId should be "me" however before you can call the Gmail API you need to use the service account "JWT flow" in your code to retrieve an oauth2 credential for the Gmail user you want to access as described in the Preparing to make an authorized API call service account domain-wide delegation doc.

    Specifically this uses your service account's private key to request to get a credential for the user you desire (set the user's email in your domain that you want to access in the setServiceAccountUser(user@example.com) function in the request). Then you can use that GoogleCredential in your call to the Gmail API.

提交回复
热议问题