Google OAuth using domain wide delegation and service account

后端 未结 1 1242
生来不讨喜
生来不讨喜 2021-01-26 14:41

I am trying to make google drive API calls using domain wide delegation by using a service account. I can get the authentication working but not the drive api calls. Error: Fi

相关标签:
1条回答
  • 2021-01-26 15:24

    Answer:

    You need to pass your Service Account private key obtained from the GCP console to your JWT Client, and specify which user you wish to impersonate as a subject.

    Code:

    After getting your private key, you need to pass this into your JWT Client before authorisation:

    let google = require('googleapis');
    let privateKey = require("./privatekey.json");
    
    var jwtClient = new google.auth.JWT({
           email: privateKey.client_email,
           key: privateKey.private_key,
           scopes: ['https://www.googleapis.com/auth/drive'],
           subject: 'user@domain.com'
        });
    
    jwtClient.authorize(function (error, tokens) {
      if (error) {
        console.log(error);
        return;
      } 
      else {
        console.log("Successfully connected!");
      }
    });
    

    Then you can do as you wish with the Drive API as the service account.

    I hope this is helpful to you!

    0 讨论(0)
提交回复
热议问题