Hereは、サーバー側の認証を設定する方法の一例です。上記のコードは、誰かがサイトにアクセスしたときに新しいトークンを作成します。そのアクセストークンを取得するエンドポイントが表示されますhere。以下は
は作業バージョンを取得するための一般的な手順は次のとおりです。
ステップ1:キーJSONをサービスアカウントを作成し、ダウンロードし
ステップ2:ユーザーとしてサービスアカウントを追加します。 Googleアナリティクスで
ステップ3:アクセストークンを要求するようにJSONのキーデータを使用し
戻るクライアント側に0
# service-account.py
import json
from oauth2client.client import SignedJwtAssertionCredentials
# The scope for the OAuth2 request.
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'
# The location of the key file with the key data.
KEY_FILEPATH = 'path/to/json-key.json'
# Load the key file's private data.
with open(KEY_FILEPATH) as key_file:
_key_data = json.load(key_file)
# Construct a credentials objects from the key data and OAuth2 scope.
_credentials = SignedJwtAssertionCredentials(
_key_data['client_email'], _key_data['private_key'], SCOPE)
# Defines a method to get an access token from the credentials object.
# The access token is automatically refreshed if it has expired.
def get_access_token():
return _credentials.get_access_token().access_token
:
ステップ4:埋め込みAPIライブラリをロードします。
<script>
(function(w,d,s,g,js,fs){
g=w.gapi||(w.gapi={});g.analytics={q:[],ready:function(f){this.q.push(f);}};
js=d.createElement(s);fs=d.getElementsByTagName(s)[0];
js.src='https://apis.google.com/js/platform.js';
fs.parentNode.insertBefore(js,fs);js.onload=function(){g.load('analytics');};
}(window,document,'script'));
</script>
ステップ5:ダッシュボード・コンポーネントをホストするHTMLコンテナを追加します。
<div id="chart-1-container"></div>
<div id="chart-2-container"></div>
ステップ6:ダッシュボードのコードを記述します。
手順3で取得したアクセストークンを使用して、Embed APIを承認します。
gapi.analytics.ready(関数(){
/**
* Authorize the user with an access token obtained server side.
*/
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '{{ ACCESS_TOKEN_FROM_SERVICE_ACCOUNT }}'
}
});
...
トークンを返すエンドポイントを作成する追加の作業が終了実装あなたにバック依存するが、どのようにデモのソースコードそれはhereです。
https://ga-dev-tools.appspot.com/embed-api/server-side-authorization/ – Matt