【问题标题】:(401) Unauthorized when trying to insert a moment using the Google+ API(401) 尝试使用 Google+ API 插入时刻时未经授权
【发布时间】:2014-12-11 07:17:12
【问题描述】:

使用下面的代码我成功收到了一个令牌,并且使用这个令牌还可以获取用户详细信息,但是当我尝试在用户墙中发布/插入时刻时,我看到以下错误消息

Fatal error: Uncaught exception 'Google_Service_Exception' 带有消息'Error calling POST https:--www.googleapis.com/plus/v1/people/me/moments/vault: (401) Unauthorized' in $_SESSION['access_token_gp']

当用户在登录页面中使用我的网站登录时,我获得了用户令牌,我请求以下权限

$client->addScope("email");
$client->addScope("https://www.googleapis.com/auth/plus.stream.write");
$client->addScope("https://www.googleapis.com/auth/plus.login");

如果我 print_r($tokenInfo);您将看到我在登录时询问的所有范围。

完整代码:

session_start();
require_once realpath(dirname(__FILE__) . '/google-api-php-client-master/autoload.php');

$client_id = 'my_client_id';
$client_secret = 'my_secret_key';
$redirect_uri = 'my_redirect_url';

// code to post in google plus start here //
$client = new Google_Client();
$client->setClientId($client_id);
$client->setClientSecret($client_secret);
$client->setRedirectUri($redirect_uri);

if (isset($_SESSION['access_token_gp'])) {
    // Verify the token
    $token = json_decode($_SESSION['access_token_gp']);
    $reqUrl = 'https://www.googleapis.com/oauth2/v1/tokeninfo?access_token='.$token->access_token;
    $req = new Google_Http_Request($reqUrl);
    $tokenInfo = get_object_vars(json_decode($client->getAuth()->authenticatedRequest($req)->getResponseBody()));
    if($tokenInfo['expires_in'] <= 0){
        $client->authenticate($_SESSION['access_token_gp']);
        $_SESSION['access_token_gp'] = $client->getAccessToken();
    } else {
        $client->setAccessToken($_SESSION['access_token_gp']);
    }

    $plusservicemoment = new Google_Service_Plus_Moment();
    $plusservicemoment->setType("http://schemas.google.com/AddActivity");

    $plusService = new Google_Service_Plus($client);

    $item_scope = new Google_Service_Plus_ItemScope();
    $item_scope->setId($tokenInfo['user_id']);
    $item_scope->setType("http://schemas.google.com/AddActivity");
    $item_scope->setName("The madexme Platform");
    $item_scope->setDescription("A page that describes just how madexme is work!");
    //$item_scope->setImage("full image path here");
    $plusservicemoment->setTarget($item_scope);
    $result = $plusService->moments->insert('me','vault',$plusservicemoment);
    //print_r($result);
}
// code to post in google plus end here //

【问题讨论】:

  • 您知道发布片刻不会发布到 Google+ 上的用户墙上,对吗?
  • 是的,我只需要在我的网站上发布一些用户帐户中的数据,例如 google+ 中的分享功能

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


【解决方案1】:

确保您拥有来自GitHub 的最新客户端库。您的 Oauth2 连接有问题。此代码部分转换自我的Google Google Calendar API 教程。我现在没有能力测试它。但这应该很接近。我今晚会测试它。

<?php    
require_once 'Google/Client.php';
require_once 'Google/Service/Plus.php';  

session_start(); 
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$client->setDeveloperKey("AIzaSyBBH88dIQPjcl5nIG-n1mmuQ12J7HThDBE");  
$client->setClientId('2046123799103-i6cjd1hkjntu5bkdkjj5cdnpcu4iju8p.apps.googleusercontent.com');
$client->setClientSecret('6s4YOx3upyJhtwnetovfK40e');
$client->setRedirectUri('http://localhost/google-api-php-client-samples/Calendar/oauth2Pure.php');
$client->setAccessType('offline');   // Gets us our refreshtoken

$client->setScopes(array(https://www.googleapis.com/auth/plus.login'));


//For loging out.
if (isset($_GET['logout'])) {
    unset($_SESSION['token']);
}


// Step 2: The user accepted your access now you need to exchange it.
if (isset($_GET['code'])) {

    $client->authenticate($_GET['code']);  
    $_SESSION['token'] = $client->getAccessToken();
    $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
    header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

// Step 1:  The user has not authenticated we give them a link to login    
if (!isset($_SESSION['token'])) {

    $authUrl = $client->createAuthUrl();

    print "<a class='login' href='$authUrl'>Connect Me!</a>";
}    
// Step 3: We have access we can now create our service
if (isset($_SESSION['token'])) {
    $client->setAccessToken($_SESSION['token']);
    print "<a class='logout' href='".$_SERVER['PHP_SELF']."?logout=1'>LogOut</a><br>";  

    $service = new Google_Service_Plus($client);    

    $moment = new Google_Moment();
    $moment->setType('http://schemas.google.com/AddActivity');
    $itemScope = new Google_ItemScope();
    $itemScope->setUrl('https://developers.google.com/+/plugins/snippet/examples/thing');
    $moment->setTarget($itemScope);
    $plus->moments->insert('me', 'vault', $moment);
?>

再次希望您明白,这不会显示在用户的 Google+ 页面/时间轴上。

【讨论】:

  • 感谢您的回复,我测试了您给定的示例,但在新的 google + api 中,新的 Google_Moment() 类不存在,因此我将其替换为新类并显示 错误调用 POST @987654323 @: (403) 访问未配置
  • 是的,您将不得不检查文档并查看客户端库。确保您在开发者控制台中启用了 Google+ API。这会给你访问未配置错误。直到以后我才有能力测试这个
  • 我从这里下载谷歌库github.com/google/google-api-php-client
  • 我查看了所有 google 文档,但没有发现使用 Google_Service_Plus_Moment() 类插入时刻的完美方法,它显示了旧版本库类示例和文档。
  • 亲爱的你能给我完整的例子吗?在 google plus 上登录和发布/时刻插入。所以我会参考并检查。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-02
  • 2016-12-14
  • 2017-11-13
  • 2014-10-22
  • 1970-01-01
相关资源
最近更新 更多