【问题标题】:Unauthorized 401 on Google+ API moments.insert with PHPGoogle+ API 上的未经授权的 401。使用 PHP 插入
【发布时间】:2014-04-03 17:38:24
【问题描述】:

我想创建一个小机器人,它可以在特定条件下将 G+ 时刻推送到 G+ 页面。想用 PHP 试试。所以我有:在 Google 控制台中注册了 Web 应用程序,打开了 Google+ API,我的 Apache 服务器上的小型 PHP 脚本和 401 Unathorized 结果。我正在使用来自https://github.com/google/google-api-php-client的google-api-php-client

我已经通过互联网搜索了答案,但没有找到任何答案。每个地方的每个人都有自己的快乐结局并且都工作,但我已经尝试了所有这些 - 只有 401 来找我。

我的脚本:

<?
header("Content-Type: text/html; charset=utf-8");
session_start();
ini_set("display_errors", 1);
require_once 'Google/Client.php';
require_once 'Google/Service/Plus.php';
$client = new Google_Client();
$client->setClientId("MY_CLIENT_ID");
$client->setClientSecret("MY_CLIENT_SECRET");
$client->setRedirectUri("REDIRECT_URL");
$client->setAccessType('offline');
$client->addScope("https://www.googleapis.com/auth/plus.login");
$client->addScope("https://www.googleapis.com/auth/plus.me");
$client->addScope("https://www.googleapis.com/auth/userinfo.profile");
$client->addScope("https://www.googleapis.com/auth/userinfo.email");
$requestVisibleActions = array('http://schemas.google.com/AddActivity');
$client->setRequestVisibleActions($requestVisibleActions);
if (!isset($_GET['code'])) {
    if (isset($_SESSION['access_token'])) {
        $client->setAccessToken($_SESSION['access_token']);
        $moment_body = new Google_Service_Plus_Moment();
        $plus = new Google_Service_Plus($client);
        $moment_body->setType("http://schemas.google.com/AddActivity");
        $item_scope = new Google_Service_Plus_ItemScope();
        $item_scope->setId("target-id-214wdefsadf1");
        $item_scope->setType("http://schemas.google.com/AddActivity");
        $item_scope->setName("The Google+ Platform");
        $item_scope->setDescription("A page that describes just how awesome Google+ is!");
        $item_scope->setImage("https://developers.google.com/+/plugins/snippet/examples/thing.png");
        $moment_body->setTarget($item_scope);
        $momentResult = $plus->moments->insert('me', 'vault', $moment_body);
    } else {
        $authUrl = $client->createAuthUrl();
        header("Location: ".$authUrl);
        exit;
    }
} else {
    $client->authenticate($_GET['code']);
    $_SESSION['access_token'] = $client->getAccessToken();
    header("Location: REDIRECT_URL");
    exit;
}

因此脚本成功地请求所有需要的访问权限,相应地在客户端注册的范围,获取令牌,将其写入会话,但是当它尝试插入新的时刻时,它得到 致命错误:未捕获的异常 'Google_Service_Exception' 和消息'错误调用 POST https://www.googleapis.com/plus/v1/people/me/moments/vault: (401) Unauthorized' in /var/www/rico/Google/Http/REST.php:79

怎么了?我已经尝试了一些在这里放置的解决方案,在 stackoveflow 上,但它们没有帮助。我还检查了我的 authURL - 看起来不错,有正确的 request_visible_actions 和其他...我不知道出了什么问题...

【问题讨论】:

  • 是否在开发者控制台 (developers.google.com/console) 中为该项目启用了 Google+ API?
  • 是的,我已经写过了
  • @RonhulMaggot 你有没有弄清楚出了什么问题?
  • 不,我必须购买可以为我完成这项工作的脚本。

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


【解决方案1】:

您可能遗漏了一些东西。也许您的示例假设您正在为您设置的其中一些设置实际值。但是,您需要设置 ApplicationName 和 DeveloperKey。如果您没有确定开发者密钥设置,许多 api 调用将返回未授权。

$client->setApplicationName($plApp->authenticationData->applicationName);
$client->setDeveloperKey($authData->apiKey);

我想你实际上是在下面设置这些。返回 url 必须与在谷歌开发者网站上为特定应用程序设置的应用程序相匹配

$client->setClientId($authData->clientId);
$client->setClientSecret($authData->clientSecret);
$client->setRedirectUri($returnUrl);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 1970-01-01
    • 2014-10-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-02
    • 2020-12-23
    相关资源
    最近更新 更多