Google Service Account Authentication PHP(谷歌服务账户认证 PHP)
问题描述
我正在尝试对服务帐户进行身份验证,以便可以将访问令牌与客户端 JSON_API 库一起使用.
I am trying to authenticate a service account so that I can use the access token with the client JSON_API library.
我看过这些文章:
https://code.google.com/p/google-api-php-client/source/browse/trunk/examples/prediction/serviceAccount.php
https://code.google.com/p/google-api-php-client/wiki/UsingTheLibrary
https://developers.google.com/storage/docs/authentication#service_accounts一>https://developers.google.com/accounts/docs/OAuth2#scenarios
这是我的 PHP 代码
Here's my PHP Code
<?php
require_once 'google-api-php-client/src/Google_Client.php';
const CLIENT_ID = "";
const SERVICE_ACCOUNT_NAME = "";
const KEY_FILE = "super secret path of course ;)";
$client = new Google_Client();
// Loads the key into PKCS 12 format
$key = file_get_contents(KEY_FILE);
$client->setAssertionCredentials(new Google_AssertionCredentials(
SERVICE_ACCOUNT_NAME,
array('https://www.googleapis.com/auth/prediction'),
$key
)
);
$client->setClientId(CLIENT_ID);
$auth = $client->authenticate();
print $auth ? "Returned true" : "Returned false";
print "<br>";
print is_null($client->getAccessToken()) ? "It's null" : "Works";
?>
这是我的输出:
返回真
是空的
推荐答案
在混合使用不同资源后,我终于弄清楚了如何使用 PHP API 库进行身份验证.
I finally figured out how to authenticate using the PHP API library after using a mixture of different resources.
这是我的 google php api 库的身份验证类
Here's my authentication class for the google php api libray
<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_StorageService.php';
class Model_Storage_Auth
{
const CLIENT_ID = "someuniquenumber.apps.googleusercontent.com";
const SERVICE_ACCOUNT_NAME = "myserviceaccountname@developer.gserviceaccount.com";
const KEY_FILE = "/supersecretpath/key.p12";
const ACCESS_TOKEN = 'access_token';
const APP_NAME = 'My App Name';
private $google_client;
function __construct()
{
$this->google_client = new Google_Client();
$this->google_client->setApplicationName(self::APP_NAME);
}
public function getToken()
{
if(!is_null($this->google_client->getAccessToken())){}
elseif(!is_null(Session::get(self::ACCESS_TOKEN, null)))
{
$this->google_client->setAccessToken(Session::get(self::ACCESS_TOKEN, null));
}
else
{
$scope = array();
$scope[] = 'https://www.googleapis.com/auth/devstorage.full_control';
$key = file_get_contents(self::KEY_FILE);
$this->google_client->setAssertionCredentials(new Google_AssertionCredentials(
self::SERVICE_ACCOUNT_NAME,
$scope,
$key)
);
$this->google_client->setClientId(self::CLIENT_ID);
Google_Client::$auth->refreshTokenWithAssertion();
$token = $this->google_client->getAccessToken();
Session::set(self::ACCESS_TOKEN, $token);
}
return $this->google_client->getAccessToken();
}
}
这篇关于谷歌服务账户认证 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:谷歌服务账户认证 PHP
基础教程推荐
- 超薄框架REST服务两次获得输出 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01