Yusaf Khaliq
Yusaf Khaliq

Reputation: 3393

Blogger google api get userid

When authenticating the user how can I get the userID in order for me to list their blogs.

<?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>";
}

The documentation is useless all it shows is how to get blogs posts etc using unknowns i.e blogID, postID etc. to be able to get blogID, postID I need the initial userID which is not given when authenticating.

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

Upvotes: 3

Views: 2008

Answers (2)

Ice
Ice

Reputation: 21

var_dump($blogger->blogs->listByUser('self'));

Upvotes: 2

Vineet1982
Vineet1982

Reputation: 7918

While doing blogger OAuth, I also come across same problem. But after reading 5-10 times the documentation it comes to me that we don't need the userID for getting the blogID. After getting the Access Token you can retrieve a list of a user's blogs by sending an HTTP GET request to the blogs collection URI:

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

or the request as:

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

Note: The user must be authenticated to list their own blogs, so you must provide the Authorization HTTP header with the GET request.

For more information visit google bloger api docs

Upvotes: 2

Related Questions