【问题标题】:401 invalid credentials with oauth 2.0 google + api ,throwing apiServiceException..why so..?oauth 2.0 google + api 的 401 无效凭据,抛出 apiServiceException ..为什么会这样..?
【发布时间】:2012-06-29 06:56:54
【问题描述】:

这就是我编写代码以通过 google 验证用户并显示基本内容的方式。

使用 google 验证用户时出错

错误:oauth 2.0 google + api 的凭据无效,抛出 apiServiceException

这是我的代码:

<?php
require_once 'google-api-php-client/src/apiClient.php';
require_once 'google-api-php-client/src/contrib/apiPlusService.php';
require_once 'google-api-php-client/src/contrib/apiOauth2Service.php';

session_start();

$client = new apiClient();

$client->setApplicationName('demo');
$client->setClientId('asd6354egfdgtdewd');
$client->setClientSecret('-ooRVhB5nbdsfisfgf7s6fsfsfj');
$client->setRedirectUri('http://demo.com');
$client->setDeveloperKey('HGFHJVhjb894rbbvjhdfjdsvkbdvdv');

$client->setScopes(array('https://www.googleapis.com/auth/plus.me',
                             'https://www.googleapis.com/auth/userinfo.email'));

$plus   = new apiPlusService($client);
$oauth2 = new apiOauth2Service($client);

if (isset($_REQUEST['logout']))
{
    unset($_SESSION['access_token']);
}

if (isset($_GET['code']))
{
    $client->authenticate();
    $_SESSION['access_token'] = $client->getAccessToken();
    header('Location: http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
            # FIXME Is exit() missing?
}

if (isset($_SESSION['access_token']))
{
    $client->setAccessToken($_SESSION['access_token']);
}

if ($client->getAccessToken())
{
        $me   = $plus->people->get('me');
        $user = $oauth2->userinfo->get();

            $email = filter_var($user['email'], FILTER_SANITIZE_EMAIL);

            $optParams = array('maxResults' => 100);
        $activities = $plus->activities->listActivities('me', 'public',$optParams);

        $_SESSION['access_token'] = $client->getAccessToken();

    } else {

           $authUrl = $client->createAuthUrl();

}

【问题讨论】:

  • 你能在 oauth 的 google 游乐场测试所有配置吗...code.google.com/oauthplayground
  • 请您指导我在 oauth 操场上进行测试..???第二步要做什么..????
  • 其实很简单。只需从左侧选择您要玩的服务,然后点击授权应用按钮,然后选择它的向导...
  • 但是在引用令牌和访问令牌中写什么..???
  • code.google.com/p/oauth-plugin/wiki/RequestToken

标签: php oauth-2.0 google-plus


【解决方案1】:

你必须在调用$plus-&gt;people-&gt;get('me')$user = $oauth2-&gt;userinfo-&gt;get()之前调用函数setAccessToken($token)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-12-09
    • 2013-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-07
    相关资源
    最近更新 更多