php invalid_grant和无效的JWT签名

i34xakig  于 2023-10-15  发布在  PHP
关注(0)|答案(1)|浏览(119)

这是我的代码,错误在最后。
我在试着获取ga追踪器代码的国别分析数据

网站上

<script async src="https://www.googletagmanager.com/gtag/js?id=G-2YFZD9DYTB"></script>
<script>
   window.dataLayer = window.dataLayer || [];
   function gtag(){dataLayer.push(arguments);}
   gtag('js', new Date());
   gtag('config', 'G-2YFZD9DYTB');
</script>

编码(已更新)

// Set up Google Client
$client = new Google_Client();
// Disable SSL verification for the Guzzle HTTP client
$httpClientConfig = [
    'verify' => false, // This disables SSL verification
];
$httpClient = new Client($httpClientConfig);
$client->setHttpClient($httpClient);
$client->setApplicationName('lookn-387014');
$client->setAuthConfig(__DIR__ . '/account-key.json');
$client->setScopes([
    AnalyticsReporting::ANALYTICS_READONLY
]);
$client->setAccessType('offline');
// Analytics Data
$analyticsData = new AnalyticsData($client);
$propertyId = '404314478'; // GA4 property ID
$dateRanges = new DateRange();
$dateRanges->setStartDate(Carbon::now()->subYear()->format('Y-m-d')); // Replace with your desired start date
$dateRanges->setEndDate(Carbon::now()->format('Y-m-d'));   // Replace with your desired end date
// Metric
$metric = new Metric();
$metric->setExpression('metrics/sessions'); // Replace with your desired metric
// Dimension
$dimension = new Dimension();
$dimension->setName('ga:country');
// Request
$request = new RunReportRequest();
$request->setProperty('property', 'properties/' . $propertyId);
$request->setDateRanges([$dateRanges]);
$request->setDimensions([$dimension]); // Set dimensions
$request->setMetrics([$metric]);

$response = $analyticsData->properties->runReport($propertyId, $request);

错误

{"error":"invalid_grant","error_description":"Invalid JWT Signature."}

服务账号密钥

我的钥匙是全新的和活跃的

更多信息

在:属性访问管理我的服务帐户已选择“管理员”标准角色

已授予角色

Creds文件

7kjnsjlb

7kjnsjlb1#

您应该使用的API称为Google Analytics Data API (GA4)

require 'vendor/autoload.php';

use Google\Analytics\Data\V1beta\BetaAnalyticsDataClient;
use Google\Analytics\Data\V1beta\DateRange;
use Google\Analytics\Data\V1beta\Dimension;
use Google\Analytics\Data\V1beta\Metric;

/**
 * TODO(developer): Replace this variable with your Google Analytics 4
 *   property ID before running the sample.
 */
$property_id = 'YOUR-GA4-PROPERTY-ID';

// Using a default constructor instructs the client to use the credentials
// specified in GOOGLE_APPLICATION_CREDENTIALS environment variable.
$client = new BetaAnalyticsDataClient();

// Make an API call.
$response = $client->runReport([
    'property' => 'properties/' . $property_id,
    'dateRanges' => [
        new DateRange([
            'start_date' => '2020-03-31',
            'end_date' => 'today',
        ]),
    ],
    'dimensions' => [new Dimension(
        [
            'name' => 'city',
        ]
    ),
    ],
    'metrics' => [new Metric(
        [
            'name' => 'activeUsers',
        ]
    )
    ]
]);

// Print results of an API call.
print 'Report result: ' . PHP_EOL;

foreach ($response->getRows() as $row) {
    print $row->getDimensionValues()[0]->getValue()
        . ' ' . $row->getMetricValues()[0]->getValue() . PHP_EOL;
}

来源:GA Data API

相关问题