簡體   English   中英

無法使用php v3庫創建或更新帖子。((403)很抱歉,您無權訪問此資源。

[英]Cannot create or update posts using php v3 library ((403) We're sorry, but you don't have permission to access this resource.')

我將Blogger庫v3用於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:

啟用Blogger Api v3

創建客戶端ID(服務帳戶):

創建客戶ID(服務帳戶)1

創建客戶ID(服務帳戶)2

請幫助我,非常感謝!

切換為使用客戶端ID(Web應用程序)而不使用客戶端ID(服務帳戶)后,我已經成功。 謝謝!

有時,如果Google Blogger ID不匹配,則可能會出現此錯誤。 您可以在博客帳戶的URL中找到博客ID。

我正在通過REST客戶端嘗試相同的操作。 似乎服務帳戶無權添加博客作者帖子。

原因可能是-只有作者/管理員可以創建/編輯帖子,並且服務電子郵件無法添加到此列表中。

附加的ARC屏幕截圖

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM