2016-07-16 13 views
0

GoogleアナリティクスAPIをノードに呼び出そうとしています。誰かが私が間違っていることを教えてもらえますか?私はnode_modules/mime/mime.jsの 'path.replace is not a function'を取得していますが、私の問題は私の認証であると確信しています。 (私のログはところでヒットされていない)google analytics api node.js

const 
    gapi  = require ("googleapis"), 
    profileid = '000000000', 
    key  = require ('./key.json'), 
    email  = '[email protected]', 
    scopes = ['https://www.googleapis.com/auth/analytics', 'https://www.googleapis.com/auth/analytics.readonly'], 
    jwt  = new gapi.auth.JWT (email, key, null, scopes, null); 

gapi.analytics ('v3').data.ga.get ({ 
    'auth': jwt, 
    'ids': 'ga:' + profileid, 
    'start-date': '30daysAgo', 
    'end-date': 'today', 
    'metrics': 'ga:pageviews' 
}, function (err, result) { 
    console.log (err, result); 
}); 

UPDATE:私は代わりにそうようにAPIキーを使用しようとしました。この場合、私のログはヒットします。しかし、私は 'ログインが必要です'というエラーが表示されます。

const 
    gapi  = require ("googleapis"), 
    API_KEY = 'IzaSyDbIBVkt5kia3CJ3w2Y3-nsLHDpSruERkw'; 

gapi.analytics ('v3').data.ga.get ({ 
    'auth': API_KEY, 
    'ids': 'ga:' + profileid, 
    'start-date': '30daysAgo', 
    'end-date': 'today', 
    'metrics': 'ga:pageviews' 
}, function (err, result) { 
    console.log (err, results); 
}); 

あなたは認証コードを取得するには、Googleアナリティクスのログインおよび範囲を必要とすると信じて私をリードhttps://developers.google.com/oauthplayground/このOAuthの遊び場。認証コードを使用すると、アクセストークンを取得できます。そして、いったんそれを持っていれば、必要なものを得るためのパラメータで要求を行うことができます。問題はコードでそれを得ることです。

承認コードの取得に関連するコードスニペットは、ユーザーが承諾ページ(ここではhttps://github.com/google/google-api-nodejs-client#retrieve-authorization-codeなど)から取得する方法で行いますが、サーバーからサーバー私はユーザーのデータを取得していないので、ユーザーからの許可は必要ないはずです。

答えて

0

サーバーサイドのリクエストを行っている場合は、cloud.google.com/consoleの "サーバーサイド"フローを通過するときにjwtルートに移動して、Googleが提供するjwt jsonファイルを使用できます。 jwtの説明についてはthese docs、スニペットについてはthis sampleを参照してください。この例ではGoogleドライブAPIを使用していますが、質問はサーバー側の認証が機能するようにすることに重点を置いています。

私はそれを動作させるために作られた関連する変更:

var authClient = new google.auth.JWT(
'[email protected]', 
'CHANGE_THIS_TO_YOUR_JSON.json', 
// Contents of private_key.pem if you want to load the pem file yourself 
// (do not use the path parameter above if using this param) 
'', 
// Scopes can be specified either as an array or as a single, space-delimited string 
['https://www.googleapis.com/auth/drive.readonly'], 
// User to impersonate (leave empty if no impersonation needed) 
''); 
0

私は次の変更で動作するように第一の方法を得ました。

  1. は、JWTの機能の代わりに
  2. ではなく、私のGoogleの電子メールのJSONファイルから電子メールを使用し、ファイル全体のJSONファイルからPRIVATE_KEYに渡さ
  3. で2番目と3番目の引数をひっくり返し
  4. は、指定したメール

    const 
        gapi  = require ("googleapis"), 
        profileid = '000000000', 
        key  = require ('./key.json'), 
        scopes = 'https://www.googleapis.com/auth/analytics.readonly', 
        jwt  = new gapi.auth.JWT (key.client_email, null, key.private_key, scopes); 
    
    jwt.authorize (function (err, response) { 
    
        gapi.analytics ('v3').data.ga.get ({ 
        'auth': jwt, 
        'ids': 'ga:' + profileid, 
        'start-date': '30daysAgo', 
        'end-date': 'today', 
        'metrics': 'ga:pageviews' 
        }, function (err, result) { 
        console.log (err, result); 
        }); 
    
    }); 
    
でGoogle Analyticsの新しいユーザーを作りました