我在这个项目中使用了laravel 5.3。我在一个帐户上有一个谷歌分析API,并有另一个Gmail帐户。我希望将api的访问权限授予第二个gmail帐户,然后我将其添加到用户。
我有一个凭证json文件,所有代码如下:
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class HomeController extends Controller {
public function index(Request $request)
{
$analytics = $this->initializeAnalytics();
$response = $this->getReport($analytics);
echo '<pre>', print_r($response) ,'</pre>';exit;
return view('index');
}
private function initializeAnalytics()
{
$client = new \Google_Client();
$client->setHttpClient(new \GuzzleHttp\Client([
'verify' => false
]));
$client->setApplicationName("Rezzta Analytics Reporting");
$client->setAuthConfig(realpath(base_path(env('GA_JSON'))));
$client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
$analytics = new \Google_Service_AnalyticsReporting($client);
return $analytics;
}
private function getReport($analytics)
{
$VIEW_ID = "xxxx";
$dateRange = new \Google_Service_AnalyticsReporting_DateRange();
$dateRange->setStartDate("7daysAgo");
$dateRange->setEndDate("today");
$sessions = new \Google_Service_AnalyticsReporting_Metric();
$sessions->setExpression("ga:sessions");
$sessions->setAlias("sessions");
$request = new \Google_Service_AnalyticsReporting_ReportRequest();
$request->setViewId($VIEW_ID);
$request->setDateRanges($dateRange);
$request->setMetrics(array($sessions));
$body = new \Google_Service_AnalyticsReporting_GetReportsRequest();
$body->setReportRequests( array( $request) );
return $analytics->reports->batchGet( $body );
}
}
但是我收到了这个错误:
Google_Service_Exception in REST.php line 118:
{
"error": {
"code": 403,
"message": "User does not have any Google Analytics account.",
"errors": [
{
"message": "User does not have any Google Analytics account.",
"domain": "global",
"reason": "forbidden"
}
],
"status": "PERMISSION_DENIED"
}
}
问题出在哪里?
答案 0 :(得分:0)
&#34;用户没有任何Google Analytics帐户。
表示您已通过身份验证的用户尚未创建Google分析帐户,或者无法访问您尝试访问的视图。
您可以通过转到管理部分下的google analytics网站授予此用户访问权限,并授予他们访问相关视图的权限。