【问题标题】:google-api-php-client-0.5.0 callbackurl?google-api-php-client-0.5.0 回调网址?
【发布时间】:2012-11-06 11:10:09
【问题描述】:

我是集成 api 的新手,我正在尝试使用 google-api-php-client-0.6.0,我在 Google 注册了我的应用程序,还在 dev.siva.com 虚拟主机的 simple.php 中配置了它们

<?php
/*
 * Copyright 2012 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.
 */

require_once 'src/Google_Client.php';
session_start();

$client = new Google_Client();
$client->setApplicationName('Google Contacts PHP Sample');
$client->setScopes("http://www.google.com/m8/contacts/");
// Documentation: http://code.google.com/apis/gdata/docs/2.0/basics.html
// Visit https://code.google.com/apis/console?api=contacts to generate your
// oauth2_client_id, oauth2_client_secret, and register your oauth2_redirect_uri.
// $client->setClientId('insert_your_oauth2_client_id');
// $client->setClientSecret('insert_your_oauth2_client_secret');
// $client->setRedirectUri('insert_your_redirect_uri');
// $client->setDeveloperKey('insert_your_developer_key');

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 (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
  $client->revokeToken();
}

if ($client->getAccessToken()) {
  $req = new Google_HttpRequest("https://www.google.com/m8/feeds/contacts/default/full");
  $val = $client->getIo()->authenticatedRequest($req);

  // The contacts api only returns XML responses.
  $response = json_encode(simplexml_load_string($val->getResponseBody()));
  print "<pre>" . print_r(json_decode($response, true), true) . "</pre>";

  // The access token may have been updated lazily.
  $_SESSION['token'] = $client->getAccessToken();
} else {
  $auth = $client->createAuthUrl();
}

if (isset($auth)) {
    print "<a class=login href='$auth'>Connect Me!</a>";
  } else {
    print "<a class=logout href='?logout'>Logout</a>";
}

我可以登录,我将重定向 url 提供给上面的代码,我正在被重定向,但问题是我无法连接页面。为什么?

重定向后的url是这样的

    https://dev.siva.com/simple.php?code=4/hqJo9FDtjcn46uuP6JXE
DuWLKTQn.Eo91flf65bgaXE-sT2ZLcbRbNNSqdQI

【问题讨论】:

  • 我过去遇到过未知问题...您有最新的 svn 更新吗? code.google.com/p/google-api-php-client/wiki/…
  • 我没有安装任何svn
  • 好的,那你可以试试。有时发布的版本不是最好的,补丁是稍后通过 SVN 提交的。所以我建议你先安装一个SVN客户端。
  • 既然你做了重定向simple.php 可能设置正确。示例项目要做的下一件事是运行$client-&gt;authenticate();,它会在服务器端调用 Google。如果客户端库无法完成此调用,则可能说明问题。您可以尝试两件事:1)检查服务器端http服务器日志。您可能会在其中找到解释根本原因的堆栈跟踪或错误。 2) 在 localhost 上启动本地 http 服务器。代码是否存在同样的问题?

标签: php api google-api


【解决方案1】:

您不能将虚拟主机用作重定向 uri - 请参阅 https://developers.google.com/accounts/docs/OAuth2InstalledApp 了解更多详细信息。所以你有两个选择 - 在 {http://localhost} 上处理它或在标题栏中获取代码而不是查询参数。我会选择第一个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-24
    • 2017-05-26
    • 2017-07-30
    • 1970-01-01
    • 2017-08-21
    • 2014-05-18
    • 1970-01-01
    • 2017-09-05
    相关资源
    最近更新 更多