php - google-api-php-client-0.5.0 回调网址?

标签 php api google-api

我是集成 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

最佳答案

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

关于php - google-api-php-client-0.5.0 回调网址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13249741/

相关文章:

PHP 向下舍入规则

php - PHP_NORMAL_READ切断正在发送的数据

node.js - 从 google doc api 保存应用程序/pdf 类型的正确方法是什么?

ruby - 将 Grape API(非 Rails)拆分为不同的文件

android - 如何在其他调用运行时以编程方式合并调用(电话 session )

google-analytics - Google Analytics(分析)如何计算每个配置文件的 10000 个请求?

javascript - Google Chart API 缺乏数据和注释

php - .htaccess 中的错误页面 - 找到请求的页面?

php - 以滴答为单位的 UTC 时间(自 01/01/0001 00 :00:00)

api - MediaWiki API 和编码