我想通过PHP脚本在Googleplus商家页面上发布片刻。
要调用Google API我正在使用服务帐户。
以下代码给出了此错误“致命错误:在(最后一行)中的非对象上调用成员函数insert()”....你能帮我解决这个问题吗?
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_PlusService.php';
// Set your client id, service account name, and the path to your private key.
// For more information about obtaining these keys, visit:
// https://developers.google.com/console/help/#service_accountsconst CLIENT_ID = 'MYID';
const SERVICE_ACCOUNT_NAME = 'MYACCOUNT';
// Make sure you keep your key.p12 file in a secure location, and isn't
// readable by others.
const KEY_FILE = 'MYAUTHFILE';
// Load the key in PKCS 12 format (you need to download this from the
// Google API Console when the service account was created.
$client = new Google_Client();
$key = file_get_contents(KEY_FILE);
$client->setClientId(CLIENT_ID);
$client->setAssertionCredentials(new Google_AssertionCredentials(SERVICE_ACCOUNT_NAME,array('https://www.googleapis.com/auth/prediction'),$key));
// Create moment that does not have a URL.
$item_scope = new Google_ItemScope();
$item_scope->setId("MYGOOGLEPAGEID");
$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 = new Google_Moment();
$moment_body->setType("http://schemas.google.com/AddActivity");
$moment_body->setTarget($item_scope);
$momentResult = $plus->moments->insert('me', 'vault', $moment_body);
答案 0 :(得分:2)
使用https://www.googleapis.com/auth/plus.login
范围时,您只能代表经过身份验证的用户撰写片刻。您无法使用服务帐户执行此操作。更多信息,您无法通过Google+信息页进行身份验证,因此无法在您的方案中撰写文章。
你认为你可以编辑你的帖子来解释为什么你想代表一个页面写片刻?你想要实现的目标是什么?
答案 1 :(得分:1)
您似乎没有初始化Plus API客户端:
$plus = new Google_PlusService($client);
您还必须使用正确的范围https://www.googleapis.com/auth/plus.login
而不是https://www.googleapis.com/auth/prediction
而且我不确定写作时刻是否适用于服务帐户......