Google Plus集成不适用于Php

时间:2019-02-25 13:08:32

标签: php api google-api google-plus google-api-php-client

我正在Google上与php集成,但是每当我点击“ mydomainname.com/example.php”时,我就会关注error_get_last

  

“ 400。那是一个错误。错误:redirect_uri_mismatch”,

在Google控制台的“授权重定向URI”部分中,我将以下网址放在 myurl / google-plus / example.php

这是我的example.php文件代码,哪里错了?

<?php
/* Require library */
require_once 'class.googleplus.php';
/* Configuration Values */
$config['consumer_key'] = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com';
$config['consumer_secret']   = 'xxxxxxxxxxxxxxxxx';
$config['callbackUrl']  = 'http://' . $_SERVER['SERVER_NAME'] . '?verify';
$GooglePlus = new GooglePlusPHP($config);
/* Verification phase */
if (!isset($_SESSION['googlePlusOAuth']) && isset($_GET['verify']) && isset($_GET['code'])):
    try {
        unset($_SESSION['googlePlusOAuth']);
        $accessToken = $GooglePlus->getAccessToken($_GET['code']);
        $GooglePlus->setOAuthToken($accessToken->access_token, false);
        $_SESSION['googlePlusOAuth'] = $accessToken;
    } catch (Exception $e) {
        die($e->getMessage());
        exit;
    }
    header('Location: example.php');
    exit;
endif;
/* No token, and no ?verify . Redirect to auth. */
if (!isset($_SESSION['googlePlusOAuth'])):
    header('Location: ' . $GooglePlus->getAuthorizationUrl() );
endif;
/* Set Access Token */
$GooglePlus->setOAuthToken($_SESSION['googlePlusOAuth']['access_token']);
if (!$GooglePlus->testAuth())
    die('Your token probably expired, or was not valid. Clear the session and try again.');

/* Profile */
$profile = $GooglePlus->getMyProfile();
/* My Activities */
$activities = $GooglePlus->getMyActivities();
/* People Search */
if (isset($_GET['search'])):
    if (isset($_GET['search_pagetoken'])):
        $search_pagetoken = $_GET['search_pagetoken'];
    else:
        $search_pagetoken = null;
    endif;
    $search_results = $GooglePlus->searchPeople($_GET['search'], $search_pagetoken);
endif;
/* Load Profile, override $activities */
if (isset($_GET['profile_id'])):
    $profile_id = $_GET['profile_id'];
    if (!is_numeric($profile_id)): continue; endif;

    $activities = $GooglePlus->getPublicActivities($profile_id);

    $user_profile = $GooglePlus->getUserProfile($profile_id);
endif;
?>
<html>
<head>
<title>Sample Google+</title>
</head>
<body>

<h1>Google+ Example Script</h1>

<h2>$profile</h2>
<pre><?php var_dump($profile); ?></pre>
<hr />

<h2>$search_results</h2>
<p>Set $_GET['search'] to view results.</p>
<pre><?php var_dump($search_results); ?></pre>
<hr />

<h2>$user_profile</h2>
<p>Set $_GET['profile_id'] to view results.</p>
<pre><?php var_dump($user_profile); ?></pre>
<hr />

<h2>$activities</h2>
<pre><?php var_dump($activities); ?></pre>

</body>
</html>

1 个答案:

答案 0 :(得分:2)

  

redirect_uri_mismatch

意味着您在Google开发人员控制台中为您的项目输入的重定向uri与您的代码使用的重定向URI不同。如果您查看该页面,则应该有一个链接,该链接会在其中确切告诉您从何处获得呼叫,您只需添加该链接即可

API关机警告

话虽这么说,您应该意识到正在使用的api将在接下来的几周内关闭,因此可能很浪费时间。在people.search

上查看页面顶部

enter image description here