无法使用php v3库创建或更新帖子((403)我们很抱歉,但您无权访问此资源。')

时间:2014-07-30 18:39:33

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

我使用Blogger库v3 for php,我无法插入新帖子。

错误消息是:

Fatal error: Uncaught exception 'Google_Service_Exception' with message 'Error calling POST https://www.googleapis.com/blogger/v3/blogs/3631076204866601722/posts: (403) We're sorry, but you don't have permission to access this resource.' in E:\xampp\htdocs\Udemy2Blogger\libs\Google\Http\REST.php:79
Stack trace:
 #0 E:\xampp\htdocs\Udemy2Blogger\libs\Google\Http\REST.php(44): Google_Http_REST::decodeHttpResponse(Object(Google_Http_Request))
 #1 E:\xampp\htdocs\Udemy2Blogger\libs\Google\Client.php(508): Google_Http_REST::execute(Object(Google_Client), Object(Google_Http_Request))
 #2 E:\xampp\htdocs\Udemy2Blogger\libs\Google\Service\Resource.php(195): Google_Client->execute(Object(Google_Http_Request))
 #3 E:\xampp\htdocs\Udemy2Blogger\libs\Google\Service\Blogger.php(1495): Google_Service_Resource->call('insert', Array, 'Google_Service_...')
 #4 E:\xampp\htdocs\Udemy2Blogger\index.php(78): Google_Service_Blogger_Posts_Resource->insert('363107620486660...', Object(Google_Service_Blogger_Post), Array)
 #5 {main} thrown inE:\xampp\htdocs\Udemy2Blogger\libs\Google\Http\REST.php on line 79

代码是:

<?php

session_start();
$path = "libs";
set_include_path(get_include_path() . PATH_SEPARATOR . $path);

require_once 'Google/Client.php';
require_once 'Google/Service/Blogger.php';

$blogId = 'xxxxxxxxxxxxxxxxxxxx';

$service_account_name = 'xxxxxxxxxxxxxxxxxxxx';
$key_file_location = 'udemy coupon free-e0de16bee449.p12';

$client = new Google_Client();
$client->setAccessType('offline');
$client->setApplicationName("Udemy coupon free");

$bloggerService = new Google_Service_Blogger($client);

/************************************************
  If we have an access token, we can carry on.
  Otherwise, we'll get one with the help of an
  assertion credential. In other examples the list
  of scopes was managed by the Client, but here
  we have to list them manually. We also supply
  the service account
 ************************************************/
if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key1 = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/blogger'),
    $key1
);

$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}

$_SESSION['service_token'] = $client->getAccessToken();
$newpost = new Google_Service_Blogger_Post();
$newpost->setTitle("test");
$newpost->setContent("test content");

$post = $bloggerService->posts->insert($blogId, $newpost, array());


print_r($post);

启用Blogger Api v3:

Enable Blogger Api v3

创建客户端ID(服务帐户):

Create client id (service account) 1

Create client id (service account) 2

请帮助我,非常感谢!

3 个答案:

答案 0 :(得分:0)

切换到使用客户端ID(Web应用程序)而不是使用客户端ID(服务帐户)后,我成功了。 谢谢!

答案 1 :(得分:0)

有时,如果Google博客ID不匹配,则可能会出现此错误。您可以在博客帐户的网址中找到博主ID。

答案 2 :(得分:0)

我正在通过REST客户端尝试相同的操作。似乎服务帐户无权添加博客作者帖子。

  

原因可能是-只有作者/管理员可以创建/编辑帖子和   服务电子邮件无法添加到此列表中。

Attached ARC screenshot