您好我正在使用谷歌分析嵌入api,起初我的代码是使用javascript工作并手动键入令牌,下面是我的代码:
<div id="embed-api-auth-container"></div>
<div id="chart-container"></div>
<div id="view-selector-container"></div>
<script type="text/javascript">
(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>
<script type="text/javascript">
gapi.analytics.ready(function() {
/**
* Authorize the user immediately if the user has already granted access.
* If no access has been created, render an authorize button inside the
* element with the ID "embed-api-auth-container".
*/
gapi.analytics.auth.authorize({
'serverAuth': {
'access_token': '<?php echo $accessToken ?>'
}
});
/**
* Create a new ViewSelector instance to be rendered inside of an
* element with the id "view-selector-container".
*/
var viewSelector = new gapi.analytics.ViewSelector({
container: 'view-selector-container'
});
// Render the view selector to the page.
viewSelector.execute();
/**
* Create a new DataChart instance with the given query parameters
* and Google chart options. It will be rendered inside an element
* with the id "chart-container".
*/
var dataChart = new gapi.analytics.googleCharts.DataChart({
query: {
ids: "xxxxxxx",
metrics: 'ga:sessions',
dimensions: 'ga:date',
'start-date': '30daysAgo',
'end-date': 'yesterday'
},
chart: {
container: 'chart-container',
type: 'LINE',
options: {
width: '100%'
}
}
});
/**
* Render the dataChart on the page whenever a new view is selected.
*/
viewSelector.on('change', function(ids) {
dataChart.set({query: {ids: ids}}).execute();
});
});
</script>
然后在我添加php代码获取刷新令牌后,整个网页变为空白。当我检查它根本没有运行时,下面是我的PHP代码
<?php
require_once( '/web/google-api-php-client-2.2.1/vendor/autoload.php' );
$accessToken = initializeAnalytics();
function initializeAnalytics()
{
$KEY_FILE_LOCATION ='/web/client_secret_917950001739-6vodhfsbpmipoh29fl3f1pkqmo7u9dhv.apps.googleusercontent.com.json';
$client = new Google_Client();
$client -> setApplicationName("testing");
$client -> setAuthConfig($KEY_FILE_LOCATION);
$client -> setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$client -> refreshTokenWithAssertion();
$client -> setAccessType("offline");
$client -> setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/testing-embed.php');
$token = $client->getAccessToken();
$accessToken = $token['access_token'];
return $accessToken;
}
echo $accessToken;
?>
任何人都知道这是什么问题?只有在我隐藏了这两行代码之后,Javascript才会运行
require_once( '/web/google-api-php-client-2.2.1/vendor/autoload.php' );
$accessToken = initializeAnalytics();
欣赏它。
更新屏幕截图: