本文介绍了Google Analytics API 401无效的凭证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何使用来访问Google Analytics。具体来说,我想 。我的代码确实能够从GA获取信息,我可以查看流量​​数据的配置文件,但我无法弄清楚如何上传。

I'm wracking my brain trying to figure out how to use the Google API PHP Client to access Google Analytics. Specifically, I want to upload cost data from other campaigns. My code does work to get information from GA, I can view traffic data for profiles, but I cannot figure out how to upload.

以下是我正在使用的代码for auth:

Here's the code that I'm using for auth:

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_AnalyticsService.php';
session_start();

$client = new Google_Client();
$client->setApplicationName("GA-Tester");
$client->setClientId('xxx.apps.googleusercontent.com');
$client->setClientSecret('xxx');
$client->setRedirectUri('http://www.site.com/indext.php');
$client->setDeveloperKey('xxx');

$analyticsService = new Google_AnalyticsService($client);
$dailyUploads = $analyticsService->management_dailyUploads;

if (isset($_GET['logout'])) {
  unset($_SESSION['token']);
}
if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}
if (isset($_SESSION['token'])) {
  $client->setAccessToken($_SESSION['token']);
}
if (!$client->getAccessToken()) {
  header ('Location:http://www.site.com/indext.php');
} else {...

此代码对请求数据有效。我可以获取帐户,配置文件列表,下载特定配置文件的流量数据等。没有任何错误或问题。

This code does work for requesting data. I can get a list of accounts, profiles, download traffic data for a specific profile, etc. No errors or issues.

当我尝试上载包含费用的CSV文件时数据我收到'401无效凭证'错误。以下是发送文件的代码:

When I try to upload my CSV file containing cost data I get a '401 Invalid Credentials' error. Here's the code that sends the file:

$send = $dailyUploads->upload(
  $accountId,
  $webPropertyId,
  $customDataSourceId,
  $start_date,
  1,
 'cost',
  array(
   "reset" => true,
   "data" => $cont,
   "mimeType" => "application/octet-stream",
   "uploadType" => "media"));

我已经仔细检查了所有我传递的变量,他们发送了正确的信息。

I've double checked all of my variables that I'm passing, they're sending the correct information.

所以这是我的问题。如果我的所有 GET 请求都没有问题,为什么我的 POST 请求会引发错误?我无法确定这是否是我的代码错误,或者是交易评论时,我遇到了,其中规定:

I just successfully uploaded cost data for the first time. In trading comments with Nicholas Pickering I came across the documentation for Management API Authorization which states that:

范围的默认值是只读的,这就是为什么我的所有 GET 请求毫无困难地工作,但我无法通过API完成任何 POST 请求。我不记得我是如何找到它的,但在PHP客户端库中完成此操作的方法是将以下行添加到客户端声明中:

The default for scope is read only, which is why all of my GET requests were working without difficulty, but I couldn't complete any POST requests through the API. I can't remember how I found it, but the way to accomplish this within the PHP client library is to append the following line to the client declaration:

$client->setScopes('https://www.googleapis.com/auth/analytics');

只要将该行添加到我的项目中,我就能够成功上载成本数据。我很高兴已经清除了这个障碍。我还有很长的路要走完成我的项目,但至少我知道它会起作用。

As soon as I added that line to my project, I was able to successfully upload cost data. I'm really glad to have cleared this hurdle. I still have a long way to go to complete my project, but at least I know that it's going to work.

这篇关于Google Analytics API 401无效的凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 05:37