gpt4 book ai didi

php - 通过 api 发布到 google plus

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:40:34 25 4
gpt4 key购买 nike

试图找到如何从 PHP 发布到 google plus wall 但即使使用 api explorer 也得到 403 at得到了

{
"error": {
"errors": [
{
"domain": "global",
"reason": "forbidden",
"message": "Forbidden"
}
],
"code": 403,
"message": "Forbidden"
}
}

我的 PHP 代码如下所示:

    $client = new \Google_Client();
$client->setApplicationName("Speerit");
$client->setClientId($appId);
$client->setClientSecret($appSecret);
$client->setAccessType("offline"); // offline access
$client->setIncludeGrantedScopes(true); // incremental auth
$client->setAccessToken(
json_encode(
array(
'access_token' => $accessToken,
'expires_in' => 3600,
'token_type' => 'Bearer',
)
)
);
$client->setScopes(
array(
"https://www.googleapis.com/auth/userinfo.email",
"https://www.googleapis.com/auth/plus.me",
"https://www.googleapis.com/auth/plus.stream.write",
)
);
$client = $client->authorize();

// create the URL for this user ID
$url = sprintf('https://www.googleapis.com/plusDomains/v1/people/me/activities');

// create your HTTP request object
$headers = ['content-type' => 'application/json'];
$body = [
"object" => [
"originalContent" => "Happy Monday! #caseofthemondays",
],
"access" => [
"items" => [
["type" => "domain"],
],
"domainRestricted" => true,
],
];
$request = new Request('POST', $url, $headers, json_encode($body));

// make the HTTP request
$response = $client->send($request);

// did it work??
echo $response->getStatusCode().PHP_EOL;
echo $response->getReasonPhrase().PHP_EOL;
echo $response->getBody().PHP_EOL;

遵循官方文档和其他帖子的一些引用

最佳答案

首先,我们需要了解免费谷歌账户有一个 API,即以 @gmail.com 结尾的账户,还有一个用于 G Suite 账户的 API,即以@ 结尾的账户yourdomain.com. 基于 reference documentation 和最近的测试,无法使用 API 在免费的谷歌帐户上插入评论 (@gmail.com)

这仅适用于 G Suite 帐户 (@yourdomain.com)。我必须阅读 insert method 的文档,我能够通过执行以下操作使其工作:

<?php session_start();

require_once "vendor/autoload.php"; //include library

//define scopes required to make the api call
$scopes = array(
"https://www.googleapis.com/auth/plus.stream.write",
"https://www.googleapis.com/auth/plus.me"
);

// Create client object
$client = new Google_Client();
$client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/index.php');
$client->setAuthConfig("client_secret.json");
$client->addScope($scopes);

if( isset($_SESSION["access_token"]) ) {

$client->setAccessToken($_SESSION["access_token"]);
$service = new Google_Service_PlusDomains($client);

$activity = new Google_Service_PlusDomains_Activity(
array(
'access' => array(
'items' => array(
'type' => 'domain'
),
'domainRestricted' => true
),
'verb' => 'post',
'object' => array(
'originalContent' => "Post using Google API PHP Client Library!"
),
)
);

$newActivity = $service->activities->insert("me", $activity);


var_dump($newActivity);


} else {

if( !isset($_GET["code"]) ){

$authUrl = $client->createAuthUrl();
header('Location: ' . filter_var($authUrl, FILTER_SANITIZE_URL));

} else {

$client->authenticate($_GET['code']);
$_SESSION['access_token'] = $client->getAccessToken();

$redirect_uri = 'http://' . $_SERVER['HTTP_HOST'] . '/index.php';
header('Location: ' . filter_var($redirect_uri, FILTER_SANITIZE_URL));

}
}

?>

总而言之,如果您尝试在免费的 gmail.com 帐户上执行此操作,您将收到 403 Forbidden 错误。希望将来可以使用此功能,但目前只有与 Google 合作的公司才能访问此特殊 API,例如 Hootsuite。

关于php - 通过 api 发布到 google plus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42742869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com