首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >google-api-php-client-0.5.0 callbackurl?

google-api-php-client-0.5.0 callbackurl?
EN

Stack Overflow用户
提问于 2012-11-06 19:10:10
回答 1查看 484关注 0票数 4

我不熟悉集成应用程序接口,我正在尝试使用google-api-php-client-0.6.0,我在谷歌注册了我的应用程序,并在dev.siva.com虚拟主机的simple.php中对它们进行了配置

代码语言:javascript
复制
<?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如下所示

代码语言:javascript
复制
    https://dev.siva.com/simple.php?code=4/hqJo9FDtjcn46uuP6JXE
DuWLKTQn.Eo91flf65bgaXE-sT2ZLcbRbNNSqdQI
EN

回答 1

Stack Overflow用户

发布于 2013-01-16 16:31:06

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13249741

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档