2017-08-21 7 views
1

Node.jsを使用してFirebase関数からGoogle Site Verification APIを使用しようとしています。 Githubの上Fire Base関数からGoogle Site Verification APIにアクセスする

GoogleがAPI-nodejsクライアントで利用可能READMEリポジトリではなく、手動でのOAuth2クライアント、JWTクライアント、または計算クライアントを作成するデフォルトのアプリケーションの方法を使用することをお勧めします。

は私がFirebase機能でローカルに(エミュレート機能環境)とリモートで実行しようとしたことを、次の例を書いた:

:実行時に、どちらの場合も

const google = require('googleapis'); 

google.auth.getApplicationDefault(function (err, authClient, projectId) { 
    if (err) { 
     console.log('Authentication failed because of ', err); 
     return; 
    } 

    if (authClient.createScopedRequired && authClient.createScopedRequired()) { 
     authClient = authClient.createScoped([ 
      'https://www.googleapis.com/auth/siteverification' 
     ]); 
    } 

    const siteVerification = google.siteVerification({ 
     version: 'v1', 
     auth: authClient 
    }); 

    siteVerification.webResource.get({ 
     id: 'test.com' 
    }, {}, function (err, data) { 
     if (err) { 
      console.log('siteVerification get error:', err); 
     } else { 
      console.log('siteVerification result:', data); 
     } 
    }); 
}); 

、私は次のエラーを取得します

siteVerification get error: { Error: A Forbidden error was returned while attempting to retrieve an access token for the Compute Engine built-in service account. This may be because the Compute Engine instance does not have the correct permission scopes specified. Insufficient Permission 
    at Request._callback (/user_code/node_modules/googleapis/node_modules/google-auth-library/lib/transporters.js:85:15) 
    at Request.self.callback (/user_code/node_modules/googleapis/node_modules/request/request.js:188:22) 
    at emitTwo (events.js:106:13) 
    at Request.emit (events.js:191:7) 
    at Request.<anonymous> (/user_code/node_modules/googleapis/node_modules/request/request.js:1171:10) 
    at emitOne (events.js:96:13) 
    at Request.emit (events.js:188:7) 
    at IncomingMessage.<anonymous> (/user_code/node_modules/googleapis/node_modules/request/request.js:1091:12) 
    at IncomingMessage.g (events.js:292:16) 
    at emitNone (events.js:91:20) 
    code: 403, 
    errors: 
    [ { domain: 'global', 
     reason: 'insufficientPermissions', 
     message: 'Insufficient Permission' } ] } 

Firebaseに関連付けられたクラウドプロジェクトでサイト検証APIが有効になっていることに注意してください。

UPDATE

info: siteVerification get error: { Error: You are not an owner of this site. 
    at Request._callback 
    ... 
    at IncomingMessage.g (events.js:292:16) 
    at emitNone (events.js:91:20) 
    code: 403, 
    errors: 
    [ { domain: 'global', 
     reason: 'forbidden', 
     message: 'You are not an owner of this site.' } ] } 

このエラーはのIDを取得するためのものです:

は、プロジェクトの所有者の役割を持つサービスアカウントを作成し、JWTの方法で認証するには、以下のアクセス権のエラーにつながります私がAPIエクスプローラを使って同じIDで電話をしたので私が所有することを知っているサイトで、これは詳細を返します。

Googleクラウドコンソールで一部の権限を設定する必要があるのか​​、認証方法が異なるのかわかりません。手動のユーザー認証を使用するOAuth 2.0クライアントのみが許可されていると感じています。

ヘルプを歓迎します。

答えて

0

サイト確認APIを使用すると、手動認証のみでOAuth 2.0を使用できます。回避策として

Your application must use OAuth 2.0 to authorize requests. No other authorization protocols are supported. If your application uses Google Sign-In, some aspects of authorization are handled for you.

、私はそれに関連したリフレッシュトークンとアクセストークンを生成:入門ドキュメントはこのについての数行が含まれています。両方の機能を使用すると、サーバー機能で使用できます。サイト確認APIに公式Google NodeJS clientを使用すると、アクセストークンの更新が管理されます。それ以外の場合は、有効期限が切れたときにアクセストークンを更新する必要があります。

以下は、簡単にアクセストークンを作成するために使用できるFirebase機能です。あなたはoauth2GetAuthorizationCodeに関連するHTTPエンドポイントを呼び出すと

function oauth2Client() { 
    return new google.auth.OAuth2(
     config.site_verification_api.client_id, 
     config.site_verification_api.client_secret, 
     'http://localhost:8080/oauth' 
    ); 
} 

exports.oauth2GetAuthorizationCode = functions.https.onRequest((req, res) => { 

    const client = oauth2Client(); 

    const url = client.generateAuthUrl({ 
     access_type: 'offline', 
     scope: [ 
      'https://www.googleapis.com/auth/siteverification' 
     ] 
    }); 

    res.status(200).send({url: url}); 
}); 

exports.oauth2GetAccessToken = functions.https.onRequest((req, res) => { 

    const client = oauth2Client(); 
    const code = req.query.code; 

    client.getToken(code, (err, tokens) => { 
     if (!err) { 
      res.status(200).send({tokens}); 
     } else { 
      console.error('Error while getting access token:', err); 
      res.sendStatus(500); 
     } 
    }); 
}); 

、URLが返されます。このURLをブラウザで開きます。これは、認証コードをクエリパラメータとして含むローカルURLにリダイレクトされます。このパラメータを取得し、oauth2GetAccessTokenに関連付けられた2番目のHTTPエンドポイントを呼び出します。この最後の呼び出しは、アクセストークンとリフレッシュトークンを返します。

あなたは両方のトークンを持っていたら、あなたは(あなたのクライアントIDとシークレットと一緒に)あなたのFirebase環境設定に保存することができますし、次のように現場検証APIにアクセス:

function oauth2ClientWithCredentials() { 
    const client = oauth2Client(); 

    client.setCredentials({ 
     access_token: config.site_verification_api.access_token, 
     refresh_token: config.site_verification_api.refresh_token 
    }); 

    return client; 
} 

function invokeSiteVerificationApi() { 

    const client = oauth2ClientWithCredentials(); 

    const siteVerification = google.siteVerification({ 
     version: 'v1', 
     auth: client 
    }); 

    siteVerification.webResource.get({ 
     id: 'dns%3A%2F%2F' + domain 
    }, null, (err, result) => { 
     // ... 
    }); 
} 
関連する問題