alxmsl /
Google服务API库
Requires
- alxmsl/cli: >=1.0.2
- alxmsl/network: >=1.1.0
Requires (Dev)
- phpunit/phpunit: 4.7.*
README
Google服务API库。支持的API
- OAuth2授权API
- Google Cloud Messaging API
- Android Publisher API: 应用内产品、购买产品和购买订阅
安装
要安装库,您需要修改您的composer配置文件
"alxmsl/googleclient": "*"
然后运行安装命令
$ composer.phar install
OAuth2授权
要通过Google OAuth2授权客户端,您需要使用控制台中的客户端标识符、客户端密钥和重定向URI创建WebServerApplication实例,并使用所需的权限
$Client = new WebServerApplication();
$Client->setClientId(<client id>)
->setClientSecret(<client secret>)
->setRedirectUri(<redirect uri>);
...制作认证URL
$Client->createAuthUrl([
'https://www.googleapis.com/auth/androidpublisher',
]
, ''
, WebServerApplication::RESPONSE_TYPE_CODE
, WebServerApplication::ACCESS_TYPE_OFFLINE
, WebServerApplication::APPROVAL_PROMPT_FORCE);
...在浏览器中完成授权并在给定的授权代码后授权。使用此代码,您可以得到访问令牌
$Token = $Client->authorizeByCode(<code>);
print((string) $Token);
您可以在webclient.uri.php中看到关于URI创建的示例,以及关于代码认证的webclient.authorize.php示例。您已经可以使用完成的脚本authorize.php
$ php bin/authorize.php
Using: /usr/local/bin/php bin/authorize.php [-h|--help] [-o|--code] -c|--client -r|--redirect -s|--scopes -e|--secret
-h, --help - show help
-o, --code - authorization code
-c, --client - client id
-r, --redirect - redirect uri
-s, --scopes - grant scopes
-e, --secret - client secret
$ php bin/authorize.php --client='my-client@id' --secret='clientsecret' --redirect='http://example.com/oauth2callback'
--scopes='https://www.googleapis.com/auth/androidpublisher'
https://#/o/oauth2/auth?response_type=code&client_id=my-client@id
&redirect_uri=http%3A%2F%2Fexample.com%2Foauth2callback&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fandroidpublisher
&access_type=offline&approval_prompt=force
$ php bin/authorize.php --client='my-client@id' --secret='clientsecret' --code='authorization/code'
access token: ya.AccES5_T0keN
expires in: 3558
id token: id.T0kEn
refresh token: refrE5H_T0KEN
token type: Bearer
当然,我已经创建了用于刷新令牌的脚本
$ php bin/refresh.php
Using: /usr/local/bin/php bin/refresh.php [-h|--help] -c|--client -e|--secret -t|--token
-h, --help - show help
-c, --client - client id
-e, --secret - client secret
-t, --token - refresh token
...以及用于撤销令牌的脚本
$ php bin/revoke.php --help
Using: /usr/local/bin/php bin/revoke.php [-h|--help] -t|--token
-h, --help - show help
-t, --token - revoked token
Google Cloud Messaging
要创建Google Cloud Message,您需要为PayloadData类创建子类,并定义getDataFields
方法。您可以在下面的示例中看到,或者在gcm.php中看到
// Create payload data class
final class NewPayloadData extends PayloadData {
protected function getDataFields() {
return [
'test' => 'test_01',
];
}
}
// Create and initialize payload message instance
$Message = new PayloadMessage();
$Message->setRegistrationIds('DeV1CeT0kEN')
->setType(PayloadMessage::TYPE_JSON)
->setData(new NewPayloadData());
// Create GCM client
$Client = new Client();
$Client->getRequest()->setConnectTimeout(60)
->setSslVersion(6);
$Client->setAuthorizationKey('aUTH0R1Z4t1oNKEy');
// ...and send the message
$Response = $Client->send($Message);
var_dump($Response);
您可以使用完成的脚本
$ php bin/gcm.php
Using: /usr/local/bin/php bin/gcm.php [-h|--help] [-d|--data] -i|--id -k|--key
-h, --help - show help
-d, --data - payload data
-i, --id - device registration id
-k, --key - authorization key
$ php bin/gcm.php --id='DeV1CeT0kEN' --key='aUTH0R1Z4t1oNKEy' --data='{"test":"test_01"}'
success: 1
failure: 0
应用内购买
您可以使用In-App products API来管理您应用程序的产品。例如,您可以为所有最终用户国家/地区获取所有产品的价格
$Client = new Client();
$Client->setPackage(<package name>)
->setAccessToken(<access token>);
/** @var InAppProductsResource $Resource */
$Resource = $Client->get(<product id>);
var_dump($Resource->getPrices());
您已经可以使用inappproducts.get.php来获取有关产品的信息
$ php bin/inappproducts.get.php --help
Using: /usr/local/bin/php bin/inappproducts.get.php [-h|--help] -a|--access [-p|--package] -r|--product
-h, --help - show help
-a, --access - access token
-p, --package - package name
-r, --product - product id
购买产品
使用Purchases.Products API,您可以在第三方服务器应用程序中检查用户的购买情况。例如,如果用户现在购买的产品尚未取消
use alxmsl\Google\AndroidPublisher\Purchases\Client;
use alxmsl\Google\AndroidPublisher\Purchases\Products\Resource as ProductsResource;
$Client = new Client();
$Client->setPackage(<package name>)
->setAccessToken(<access token>);
/** @var ProductsResource $Resource */
$Resource = $Client->get(<product id>, <purchase token>);
var_dump($Resource->isPurchased() && !$Resource->isCancelled());
购买订阅
此库允许使用一些脚本对用户的订阅进行所有操作:获取、取消、延迟、退款和撤销
如何检查订阅,例如
use alxmsl\Google\AndroidPublisher\Purchases\Subscription\Resource as SubscriptionsResource;
use alxmsl\Google\AndroidPublisher\Purchases\Subscription\SubscriptionsClient;
$Client = new SubscriptionsClient();
$Client->setPackage(<package name>)
->setAccessToken(<access token>);
/** @var SubscriptionsResource $Resource */
$Resource = $Client->get(<subscription id>, <purchase token>);
var_dump($Resource->isAutoRenewing() && !$Resource->isExpired());
测试
要运行完全测试,只需调用phpunit
命令
$ phpunit
PHPUnit 4.7.3 by Sebastian Bergmann and contributors.
..........................................
Time: 118 ms, Memory: 7.25Mb
OK (42 tests, 365 assertions)
许可证
版权所有 2015 Alexey Maslov alexey.y.maslov@gmail.com
根据Apache许可证第2版(“许可证”)授权;除非适用法律要求或书面同意,否则不得使用此文件,除非遵守许可证。您可以在以下位置获得许可证副本:
https://apache.ac.cn/licenses/LICENSE-2.0
除非适用法律要求或书面同意,否则根据许可证分发的软件按“原样”基础分发,不提供任何形式的保证或条件,无论是明示的还是暗示的。有关许可证的具体语言、权限和限制,请参阅许可证。