尝试使用PHP连接到OAuth 2.0时出现奇怪错误

时间:2014-05-15 02:10:45

标签: php oauth google-analytics google-analytics-api google-api-php-client

我已下载适用于PHP的Google API库客户端,现在正尝试与OAuth建立连接。但是,使用 service-account.php 中的以下代码:

<?php
/*
 * Copyright 2013 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
session_start();
include_once "templates/base.php";

/************************************************
  Make an API request authenticated with a service
  account.
 ************************************************/
set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Books.php';

/************************************************
  ATTENTION: Fill in these values! You can get
  them by creating a new Service Account in the
  API console. Be sure to store the key file
  somewhere you can get to it - though in real
  operations you'd want to make sure it wasn't
  accessible from the webserver!
  The name is the email address value provided
  as part of the service account (not your
  address!)
  Make sure the Books API is enabled on this
  account as well, or the call will fail.
 ************************************************/
$client_id = 'CLIENT-ID-GENERATED-FROM-GOOGLE-DEVELOPERS-CONSOLE.apps.googleusercontent.com';
$service_account_name = 'test';
$key_file_location = 'PRIVATE-KEY-FROM-GOOGLE-DEVELOPERS-CONSOLE-privatekey.p12';

echo pageHeader("Service Account Access");
if ($client_id == '<YOUR_CLIENT_ID>'
    || !strlen($service_account_name)
    || !strlen($key_file_location)) {
  echo missingServiceAccountDetailsWarning();
}

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new Google_Service_Books($client);

/************************************************
  If we have an access token, we can carry on.
  Otherwise, we'll get one with the help of an
  assertion credential. In other examples the list
  of scopes was managed by the Client, but here
  we have to list them manually. We also supply
  the service account
 ************************************************/
if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/analytics.readonly'),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();

/************************************************
  We're just going to make the same call as in the
  simple query as an example.
 ************************************************/
$optParams = array('filter' => 'free-ebooks');
$results = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
echo "<h3>Results Of Call:</h3>";
foreach ($results as $item) {
  echo $item['volumeInfo']['title'], "<br /> \n";
}

echo pageFooter(__FILE__);

我收到此错误:

  

致命错误:未捕获的异常&#39; Google_Auth_Exception&#39;有消息&#39;错误刷新OAuth2令牌,消息:&#39; {&#34;错误&#34; :&#34; invalid_grant&#34; }&#39;&#39;在/home/texterx1/public_html/google-api-php-client-master/src/Google/Auth/OAuth2.php:327堆栈跟踪:#0 / home / texterx1 / public_html / google-api-php-client-master /src/Google/Auth/OAuth2.php(289):Google_Auth_OAuth2-&gt; refreshTokenRequest(数组)#1 /home/texterx1/public_html/google-api-php-client-master/examples/service-account.php(75 )::第327行/home/texterx1/public_html/google-api-php-client-master/src/Google/Auth/OAuth2.php中引用的Google_Auth_OAuth2-&gt; refreshTokenWithAssertion(Object(Google_Auth_AssertionCredentials))#2 {main} < / p>

我做错了什么?

怀疑

我怀疑的是,我已将错误的值复制/粘贴到$client_id$service_account_name$key_file_location。我从字段服务帐户客户端ID $client_id获得了$service_account_name,我从同意屏幕&gt;获取了$key_file_location当我点击生成服务帐户下面的新密钥时,下载到我电脑的.p12文件上的文件名中的产品名称和{{1}}。我首先在我的计算机上打开.p12文件,然后按照指南进行操作,例如导入证书(不确定我是否应该这样做)。

0 个答案:

没有答案