通过 api 发布到 google
posting to google plus through api
试图找到如何从 PHP post 到 google 加墙,但即使使用 api 资源管理器也得到 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;
遵循官方文档和其他 posts
的一些参考
首先,我们需要了解有一个 API 免费 google 帐户,意思是帐户结尾为 @gmail.com 并且有是 G Suite 帐户的 API,表示以@yourdomain.com. 结尾的帐户基于 reference documentation和最近的测试,无法在免费 google 帐户 (@gmail.com).[=13= 上使用 API 插入评论]
这仅适用于 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 禁止访问错误。希望将来可以使用,但目前只有与 Google 合作的公司才能使用这种特殊的 api,例如 Hootsuite。
试图找到如何从 PHP post 到 google 加墙,但即使使用 api 资源管理器也得到 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;
遵循官方文档和其他 posts
的一些参考首先,我们需要了解有一个 API 免费 google 帐户,意思是帐户结尾为 @gmail.com 并且有是 G Suite 帐户的 API,表示以@yourdomain.com. 结尾的帐户基于 reference documentation和最近的测试,无法在免费 google 帐户 (@gmail.com).[=13= 上使用 API 插入评论]
这仅适用于 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 禁止访问错误。希望将来可以使用,但目前只有与 Google 合作的公司才能使用这种特殊的 api,例如 Hootsuite。