Google Analytics API 401无效的凭证 [英] Google Analytics API 401 Invalid Credentials

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

问题描述

我试图弄清楚如何使用 Google API PHP Client 来访问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 请求会引发错误?我无法确定这是否是我的代码错误,或者是 API控制台。任何人都可以引导我朝正确的方向发展吗?

So here's my question. If all of my GET requests work without issue, why would my POST request throw an error? I can't tell if this is an error with my code, or with settings in the API console. Can anyone steer me in the right direction?

编辑:
这里是生成的错误代码(减去可识别的位) p>

Here's the error code generated (minus identifiable bits).

Fatal error: Uncaught exception 'Google_ServiceException' with message 
'Error calling POST https://www.googleapis.com/upload/analytics/v3/man
agement/accounts/1234567/webproperties/UA-1234567-1/customDataSources/
xXxXxX/dailyUploads/2013-01-17/uploads?appendNumber=1&type=cost&reset=
true&uploadType=media&key=xXxXxX: (401) Invalid Credentials' in /../pub
lic_html/proj/google-api-php-client/src/io/Google_REST.php:66 Stack tra
ce: #0 /../public_html/proj/google-api-php-client/src/io/Google_REST.ph
p(36): Google_REST::decodeHttpResponse(Object(Google_HttpRequest)) #1 /
../public_html/proj/google-api-php-client/src/service/Google_ServiceRes
ource.php(186): Google_REST::execute(Object(Google_HttpRequest)) #2 /..
/public_html/proj/google-api-php-client/src/contrib/Google_AnalyticsSer
vice.php(82): Google_ServiceResource->__call('upload', Array) #3 /../pu
blic_html/proj/dailyUpload_send.php(60): Google_ManagementDailyUploadsS
erviceResource->upload('1234567', 'UA-1234567-1', 'xXxXxXxXxXxXxXx...',
 ' in /../public_html/proj/google-api-php-client/src/io/Google_REST.php
  on line 66


推荐答案

我第一次成功上传了成本数据。在用尼古拉斯皮克林交易评论时,我遇到了 Management API Authorization ,其中规定:

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:


如果您的 access_token 已过期或您使用的是错误的,您将获得 401 状态码

You will get a 401 status code if your access_token has expired or if you are using the wrong scope for the API.

范围的默认值是只读的,这就是为什么我的所有 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无效的凭证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆