2

Node.jsサーバーを使用してドライブ上のフォルダからファイルをダウンロード/アップロード/削除する必要があります。そのフォルダは会社のGスイートの中にあり、会社内の少数の人しかアクセスできない。ドメイン内のGoogleドライブAPIサービスアカウント

これを行うにはサービスアカウントを使用する必要があります。問題は可能ですか?どうやってやるの?

は、私はすでにhttps://developers.google.com/drive/v2/web/delegationhttps://developers.google.com/identity/protocols/OAuth2ServiceAccount を読みますが、それは会社のドメイン内のフォルダにアクセスするためのサービスアカウントに可能な弾力権限をあるかどうかは知りませんが、サービスアカウントがあるため、@ developer.gserviceaccount.comと会社のドメインは他のものなので、そのサービスアカウントをフォルダに追加しようとするとエラーが表示されます。

私にこれを導くことができれば、私は非常に素晴らしいでしょう。

ありがとうございます!

+0

技術を権利を使用することです企業ドメイン上の(任意の)ユーザーアカウントを偽装するサービスアカウントとして持っています。リンクしたコード例では、使用する "userEmail"変数です。 –

+0

@PeterHerrmannとthats all?ドメインワイドの委任を持つサービスアカウントが必要なので、会社のすべてのユーザーのファイルにアクセスできます。 – MartinGian

+0

はい。質問に回答したい特定の質問が含まれるように質問を更新してください。 –

答えて

0

あなたは権利の範囲(複数可)でのOAuthトークンを使用することができます。

const path = require('path'); 

module.exports = (app) => { 
    const factory = {}; 
    factory.connect = (done) => { 
     const fs = require('fs'); 
     const google = require('googleapis'); 
     const googleAuth = require('google-auth-library'); 

     const SCOPES = [ 
      'https://www.googleapis.com/auth/drive.metadata.readonly' 
     ]; 
     const TOKEN_DIR = path.resolve(app.root, 'server','config'); 
     const TOKEN_PATH = path.resolve(TOKEN_DIR,'token.json'); 

     const creds = require(path.resolve(app.root, 'server', 'config', 'google_oauth.json')); 
     authorize(creds, (ret) => { 
      done(null, ret); 
     }); 

     /** 
     * Create an OAuth2 client with the given credentials, and then execute the 
     * given callback function. 
     * 
     * @param {Object} credentials The authorization client credentials. 
     * @param {function} callback The callback to call with the authorized client. 
     */ 
     function authorize(credentials, callback) { 
      const clientSecret = credentials.installed.client_secret; 
      const clientId = credentials.installed.client_id; 
      const redirectUrl = credentials.installed.redirect_uris[0]; 
      const auth = new googleAuth(); 
      const oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl); 

      // Check if we have previously stored a token. 
      fs.readFile(TOKEN_PATH, function (err, token) { 
       if (err) { 
        console.error('[ERROR] Unable to read token', err) 
        getNewToken(oauth2Client, callback); 
       } else { 
        oauth2Client.credentials = JSON.parse(token); 
        callback(oauth2Client); 
       } 
      }); 
     } 

     /** 
     * Get and store new token after prompting for user authorization, and then 
     * execute the given callback with the authorized OAuth2 client. 
     * 
     * @param {google.auth.OAuth2} oauth2Client The OAuth2 client to get token for. 
     * @param {getEventsCallback} callback The callback to call with the authorized 
     *  client. 
     */ 
     function getNewToken(oauth2Client, callback) { 
      const authUrl = oauth2Client.generateAuthUrl({ 
       access_type: 'offline', 
       scope: SCOPES 
      }); 
      console.log('Authorize this app by visiting this url: ', authUrl); 
      const readline = require('readline'); 
      const rl = readline.createInterface({ 
       input: process.stdin, 
       output: process.stdout 
      }); 
      rl.question('Enter the code from that page here: ', function (code) { 
       rl.close(); 
       oauth2Client.getToken(code, function (err, token) { 
        if (err) { 
         console.log('Error while trying to retrieve access token', err); 
         return; 
        } 
        oauth2Client.credentials = token; 
        storeToken(token); 
        callback(oauth2Client); 
       }); 
      }); 
     } 

     /** 
     * Store token to disk be used in later program executions. 
     * 
     * @param {Object} token The token to store to disk. 
     */ 
     function storeToken(token) { 
      try { 
       fs.mkdirSync(TOKEN_DIR); 
      } catch (err) { 
       if (err.code != 'EEXIST') { 
        throw err; 
       } 
      } 
      fs.writeFile(TOKEN_PATH, JSON.stringify(token)); 
      console.log('Token stored to ' + TOKEN_PATH); 
     } 

    }; 
    return factory 
}; 

、その後factory.connect(done)googleapisを使用するdoneauthを与える:

   const google = require('googleapis'); 
       const service = google.drive('v3'); 
       service.files.list({ 
        auth, 
        pageSize: 10, 
        fields: 'nextPageToken, files(id, name)' 
       }, step); 
関連する問題