簡體   English   中英

Google api客戶端php 403禁止錯誤

[英]google api client php 403 forbidden error

我為此項目使用laravel 5.3。 我在一個帳戶上有一個Google Analytics(分析)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"
}
}

問題出在哪兒 ?

“用戶沒有任何Google Analytics(分析)帳戶。

表示您已通過身份驗證的用戶尚未創建Google Analytics(分析)帳戶,或無法訪問您嘗試訪問的視圖。

您可以通過訪問admin部分下的google analytics網站並授予他們訪問相關視圖的權限來授予該用戶訪問權限。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM