【问题标题】:Blogger google api get useridBlogger google api 获取用户ID
【发布时间】:2013-08-01 19:23:36
【问题描述】:

在验证用户身份时,我如何获取用户 ID 以便我列出他们的博客。

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_BloggerService.php'; 
session_start();
$client = new Google_Client();
$client->setApplicationName('Blogger');
$client->setClientId('insert_your_oauth2_client_id');
$client->setClientSecret('insert_your_oauth2_client_secret');
$client->setRedirectUri('insert_your_oauth2_redirect_uri');
$client->setDeveloperKey('insert_your_simple_api_key');
$blogger = new Google_BloggerService($client);

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 ($client->getAccessToken()) {
    var_dump($blogger->blogs->listByUser("USER_ID_HERE")); //how do i get the user id?
} else {
  $authUrl = $client->createAuthUrl();
  print "<a href='$authUrl'>Connect Me!</a>";
}

文档是无用的,它显示的只是如何使用未知数(即 blogID、postID 等)获取博客帖子等,以便能够获取 blogID、postID 我需要身份验证时未提供的初始用户 ID。

https://developers.google.com/blogger/docs/3.0/using

【问题讨论】:

  • 对不起,在一个普通文件中应该提到我的不好!
  • listByUser 函数接收 $userId @param string $userId ID of the user whose blogs are to be fetched. Either the word 'self' (sans quote marks) or the user's profile identifier.

标签: php oauth-2.0 blogger google-api-php-client


【解决方案1】:

在做博客 OAuth 时,我也遇到了同样的问题。但是在阅读了 5-10 次文档后,我发现我们不需要用户 ID 来获取 blogID。获取访问令牌后,您可以通过向博客集合 URI 发送 HTTP GET 请求来检索用户博客列表:

https://www.googleapis.com/blogger/v3/users/{userId}/blogs

或请求为:

GET https://www.googleapis.com/blogger/v3/users/self/blogs
Authorization: /* OAuth 2.0 token here */

注意:用户必须通过身份验证才能列出他们自己的博客,因此您必须在 GET 请求中提供 Authorization HTTP 标头。

欲了解更多信息,请访问google bloger api docs

【讨论】:

  • 能否请您包括如何使用 PHP 完成,我已经尝试了所有方法
【解决方案2】:

var_dump($blogger-&gt;blogs-&gt;listByUser('self'));

【讨论】:

  • 请多解释一下你的答案
  • 没有办法让用户ID代替“USER_ID_HERE”。但是我们可以从用户的访问令牌中获取用户的博客列表。
猜你喜欢
  • 2023-04-04
  • 2016-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-11
  • 1970-01-01
  • 2015-06-04
  • 1970-01-01
相关资源
最近更新 更多