标签: google-api-php-client

使用服务帐户验证Google InAppPurchase

我在最后几天尝试了很多东西,但现在我没有想法:(

我想验证在我的Android应用程序中创建的inAppPurchase.

1)我在google API控制台中创建了一个新的服务帐户.

1a)服务帐户列出了非权限,并具有"可以查看"权限

2)我使用的是最新版本的https://github.com/google/google-api-php-client

3)PHP脚本中的代码片段:

$client = new Google_Client(); 
$client->setApplicationName('myAppName' );
$client->setClientId('123456789123-vxoasdt8qwe6awerc9ysdfmjysdfysf64werweria8fh.apps.googleusercontent.com');
$key = file_get_contents('/shr/data/stor/b516cexx3123asdf3988345d8133e7f86bfas2553-privatekey.p12');
$service_account_name = '123456789123-vxoasdt8qwe6awerc9ysdfmjysdfysf64werweria8fh@developer.gserviceaccount.com';

$client->setScopes(array('https://www.googleapis.com/auth/androidpublisher') );
$cred = new Google_Auth_AssertionCredentials( $service_account_name, array('https://www.googleapis.com/auth/androidpublisher'), $key );
$client->setAssertionCredentials($cred);

try {  
    $service = new Google_Service_AndroidPublisher( $client ); 
    $googleApiResult = $service->inapppurchases->get($externalAppId, $externalProductId, $purchaseToken);
} catch (Exception $e) {  
    var_dump( $e->getMessage() ); 
}
Run Code Online (Sandbox Code Playgroud)

4)Google的回复:

获取https://www.googleapis.com/androidpublisher/v1.1/applications/de.test.myapp/inapp/de.test.inapp.google.balance5eur/purchases/[PURCHASETOKEN] :( 401)当前用户已拥有权限不足以执行请求的操作.

[PURCHASETOKEN]是我从谷歌收到的购买代币

5)将$ cred-> sub ='foo@bar.de'设置为我的邮件地址

刷新OAuth2令牌时出错,消息:'{"error":"unauthorized_client","error_description":"请求中未经授权的客户端或范围".}"

php api android publisher google-api-php-client

16
推荐指数
1
解决办法
1万
查看次数

Google API PHP离线访问,"invalid_grant:代码已被兑换"

如何在用户撤销授权之前永久授权Google客户端?

我正在尝试创建一个连接到Google日历的应用.它必须在PHP中运行,因此我使用谷歌提供的Google API PHP客户端.

该应用程序需要具有脱机访问权限,以便在用户不在会话中时可以使用.该应用程序旨在让用户在网站等上公开管理和显示他的日历.

我使用服务方法(具有客户端ID和客户端密钥)在Google控制台中创建了凭据.使用Google API客户端,我还要求用户授权.我打开一个新的浏览器窗口,用户授权,Google会返回授权码.我使用此代码,存储并使用它来授权客户端,该客户端成功连接到Google日历并交换数据.

现在,我明白这是一个即将到期的令牌.除非我使用离线访问,我设置.但是,几分钟或更短时间后,我总会收到错误:Error fetching OAuth2 access token, message: 'invalid_grant: Code was already redeemed.

这是我用来连接客户端的代码:

$client = new \Google_Client();
$client->setApplicationName( 'My App' );
$client->setScopes( array( \Google_Service_Calendar::CALENDAR ) );
$client->setClientId( $this->google_client_id );
$client->setClientSecret( $this->google_client_secret );
$client->setRedirectUri( $this->google_client_redirect );
$client->setAccessType( 'offline' );

if ( $code = $this->google_client_auth ) {

    try {

        $client->authenticate( $code );

    } catch( \Exception $e ) {
            var_dump( $e );
    }    

}

return new \Google_Service_Calendar( $client );
Run Code Online (Sandbox Code Playgroud)

这是一个类中的方法.

客户端ID和客户端密钥存储在应用程序设置中.

我也将用户返回的代码存储在一个设置中,但我认为这是我做错的地方?我在一个单独的方法(也使用相同的客户端ID)中将链接放到Google OAuth窗口并秘密和设置离线方法).并获得授权工作.我可以到达日历.它只是不会持续很长时间...

php oauth google-api google-oauth google-api-php-client

16
推荐指数
1
解决办法
2万
查看次数

Google API:找不到404域名

我是使用Google API的新手,但我有一个项目要求我访问他们的域名以通过电子邮件查找用户的经理.在我开始编写代码之前,我想要设置所有内容,因此我按照PHP 的示例文件进行操作.我能够让它工作,但是在令牌过期时刷新令牌有一些问题并且研究推动我使用服务帐户,因为这是一个服务器cron脚本,我不想处理任何用户交互.

我创建了服务帐户,启用了G Suite域范围的委派,并为以下内容添加了访问权限: https://www.googleapis.com/auth/admin.directory.user.readonly

我得到了一个Google_Service_Exception脚本.

回应是:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "notFound",
    "message": "Domain not found."
   }
  ],
  "code": 404,
  "message": "Domain not found."
 }
}
Run Code Online (Sandbox Code Playgroud)

我假设这意味着它不知道帐户域,但我不知道如何解决这个问题.我认为,如果这是一个权限问题,谷歌会告诉我.我尝试在线搜索,但没有运气,因为我发现的问题是使用不同的方法,并且修复不是可以在服务帐户上完成的.我现在被卡住了,所以我希望朝着正确的方向努力让我走上正轨.

这是我正在使用的测试脚本:

<?php

require_once( __DIR__. '/vendor/autoload.php' );

define('CREDENTIALS_PATH', '/path/to/service_account.json');

define('SCOPES', implode(' ', array(
        Google_Service_Directory::ADMIN_DIRECTORY_USER_READONLY)
));

date_default_timezone_set('America/New_York');

/**
 * Returns an authorized API client.
 * @return Google_Client the authorized client object
 */
function getClient() {
    $client = new Google_Client();
    $client->setApplicationName('TestingApp');
    $client->setAuthConfig(CREDENTIALS_PATH);
    $client->setScopes(SCOPES);

    return …
Run Code Online (Sandbox Code Playgroud)

php google-api google-api-php-client

16
推荐指数
1
解决办法
5638
查看次数

Google Calendar API库(PHP) - 文档在哪里?

我一直在寻找来自PHP Google客户端库的Google Calendar API功能,方法等的文档,但不能为我的生活找到该死的东西.有谁知道它住在哪里?我进入了小组,但有一个2个月大的问题同样的事情:

https://groups.google.com/forum/?fromgroups=#!topic/google-api-php-client/pP-E1noaqsI

这里有什么想法?

php google-calendar-api google-api google-api-php-client

15
推荐指数
1
解决办法
6513
查看次数

Google OAuth 2.0"错误":"redirect_uri_mismatch"

我已经花了一天,撞了一杯,我真的很生气,我不明白谷歌想要什么,有什么不对.

我在开发者控制台中启用了Google+ Api google_ api已启用 ,创建了新的OAuth客户端ID 客户ID

    $ch = curl_init('https://accounts.google.com/o/oauth2/token');
curl_setopt($ch,CURLOPT_POSTFIELDS,'code=4%2FPp1GWqC6PIr3wNYrZ5kK4T9oitxBoo0fBqBrVNQfE-g.ElKDUjQ7E28SoiIBeO6P2m-0RPaolgI&grant_type=authorization_code&redirect_uri=https%3A%2F%2Fmyprivatedomain.local.com%2Foauth2callback&client_id=%mycliet_id%&client_secret=%mysecret%');
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, true);
var_dump(curl_exec($ch));
Run Code Online (Sandbox Code Playgroud)

在此处的说明中创建了所有内容:https://developers.google.com/+/web/signin/server-side-flow,gplus按钮显示在页面上,并成功请求授权用户访问.但是,当我执行第8 步时:初始化Google API客户端库并在 每次获得响应"错误"启动Google+服务我的请求:"redirect_uri_mismatch"

我知道,当您未在Google控制台中注册redirect_uri时,或者当您在其中输入类型错误时,会出现此错误,但我已将其注册,并且仅用于测试尝试设置不同的网址(更改的域名,更改的协议来自https到https),但它永远不会工作!我不知道还能检查什么,请至少建议一下.

oauth google-api-php-client google-oauth2

14
推荐指数
1
解决办法
6万
查看次数

Google API客户端"刷新令牌必须传入或设置为setAccessToken的一部分"

我目前面临一个非常奇怪的问题,实际上我一直在从Google API文档中遵循这个相同的指南(https://developers.google.com/google-apps/calendar/quickstart/php).我尝试了两次,第一次它像魅力一样工作但是在访问令牌过期后,Google API Doc直接提供的脚本无法刷新它.

TL; DR

这是错误消息:

sam@ssh:~$ php www/path/to/app/public/quickstart.php


Fatal error: Uncaught exception 'LogicException' with message 'refresh token must be passed in or set as part of setAccessToken' in /home/pueblo/www/path/to/app/vendor/google/apiclient/src/Google/Client.php:258
Stack trace:
#0 /home/pueblo/www/path/to/app/public/quickstart.php(55): Google_Client->fetchAccessTokenWithRefreshToken(NULL)
#1 /home/pueblo/www/path/to/app/public/quickstart.php(76): getClient()
#2 {main}
  thrown in /home/pueblo/www/path/to/app/vendor/google/apiclient/src/Google/Client.php on line 258
Run Code Online (Sandbox Code Playgroud)

这是我修改的谷歌php脚本的一部分:

require_once __DIR__ . '/../vendor/autoload.php';

// I don't want the creds to be in my home folder, I prefer them in the app's root
define('APPLICATION_NAME', 'LRS API Calendar');
define('CREDENTIALS_PATH', __DIR__ . …
Run Code Online (Sandbox Code Playgroud)

php google-calendar-api google-api access-token google-api-php-client

14
推荐指数
2
解决办法
2万
查看次数

使用PHP使用JWT为Google云端存储签名URL

我刚刚开始将我的Google云存储代码从API 1.0版升级到2.0版,我遇到了一些麻烦.

在1.0版本中,我使用签名URL,使用.p12文件取得了巨大成功.然而,在新版本中已弃用,我必须使用Firebase/php-jwt,而不是使用JSON文件.

问题是它只是不工作,我收到错误:

<?xml version='1.0' encoding='UTF-8'?><Error><Code>SignatureDoesNotMatch</Code>
<Message>The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.</Message>
<StringToSign>PUT

image/png
1483626991
/myBucket/folder/test.PNG</StringToSign></Error>
Run Code Online (Sandbox Code Playgroud)

这是用于签名的简化代码.

$string = ($method . "\n" .
          $contentMd5 . "\n" .
          $contentType . "\n" .
          $expiration . "\n" .
          $file);

$signedURL = base64_encode(Firebase\JWT\JWT::encode($string,
        file_get_contents($credentialsFilePath)));
Run Code Online (Sandbox Code Playgroud)

收到signedURL后,我使用正确的数据构建一个URL.我从1.0和2.0更改的唯一部分是您对URL进行签名的部分.此外,我已经检查了响应的"StringToSign"字段中的字符串与我正在签名的字符串完全相同.

在1.0版本中,我签署了这样的URL:

$signedURL = base64_encode((new Google_Signer_P12(
        file_get_contents($p12FilePath),
        'notasecret'
      ))->sign($string));
Run Code Online (Sandbox Code Playgroud)

所有这一切让我相信我正在唱出正确的内容,但是以错误的方式使用JWT功能.还有其他人这样做过吗?你是怎么做到的?

万一有趣的是这是我构建的URL(与1.0一起使用):

$returnArr['url'] = "https://{$bucket}.commondatastorage.googleapis.com/"
    . $prefix . '/' . rawurlencode($file)
    . "?GoogleAccessId=" . rawurlencode($serviceEmail)
    . …
Run Code Online (Sandbox Code Playgroud)

php cloud google-cloud-storage google-api-php-client

14
推荐指数
1
解决办法
859
查看次数

如何在Google Developers Console中创建项目

我正在尝试在Google Developers Console中创建一个项目.

注意:我是域管理员.

我不断收到此错误消息;

错误

尚未为您的帐户激活开发者控制台.您的帐户可能被暂停或停用.如果您是Google Apps用户,请让您的域管理员在您的帐户中启用Apphosting Admin.

请参阅我的步骤以产生错误(屏幕截图): https ://drive.google.com/file/d/0Bytqhoir_Tt5QmFTazM0SzZONU0/view ? usp = sharing https://drive.google.com/file/d/0Bytqhoir_Tt5c3BwSnA3bThCSXc/view ?usp =分享https://drive.google.com/file/d/0Bytqhoir_Tt5SVZZWTRfQVBTRUk/view?usp=sharing

任何人都可以分享一下如何解决这个问题吗?请注意,我是域管理员.

google-calendar-api google-api google-oauth google-api-php-client

13
推荐指数
2
解决办法
1万
查看次数

api密钥,客户端ID和服务帐户有什么区别?

我需要从我的Symfony 2应用程序访问Google的服务,即Google Analytics,因此我必须使用Google api客户端(版本2).在访问Google Analytics的信息之前,我必须在Google API控制台中创建API密钥,客户端ID或服务帐户.

最后,我创建了一个服务帐户,并下载了一个文件.Google api客户端使用此文件授予对我的Google Analytics帐户及其各自收集信息的访问权限.

我的问题是:

  1. api密钥,客户端ID和服务帐户有什么区别?

  2. 什么时候创建/使用一个,为什么?

我没有看到任何详尽的文章解释了我在这个问题上的要求.

php google-analytics symfony google-api-php-client difference

12
推荐指数
2
解决办法
6978
查看次数

无需登录即可访问Google My Business API(使用服务帐户)

我想访问与我的帐户及其评论相关联的位置,因为我使用的是Google my business API,我可以访问它(它可以在oAuthplayground上运行).

现在我想访问google my business api而不登录我的帐户,因为我试图让它与服务帐户一起使用.但到目前为止还没有运气,请建议如何继续这样做.我在服务帐户中启用了G套件,并且我还尝试访问我的业务管理的服务帐户电子邮件(ID),但它仍然处于" 邀请"状态,因为无法实际接受邀请.

当我尝试使用我的帐户作为主题发送请求时.

$client = new Google_Client();
$client->addScope('https://www.googleapis.com/auth/plus.business.manage');
$client->setAuthConfig(dirname(__FILE__) . '/Xyz Review API-service account.json');
$client->setSubject('xyz*****abc@gmail.com');
$business_service_class = new Google_Service_Mybusiness($client);
$result_accounts = $business_service_class->accounts->listAccounts();
echo json_encode($result_accounts);
exit;
Run Code Online (Sandbox Code Playgroud)

回复:{"nextPageToken":null}

如果我在主题中使用Google服务帐户ID作为电子邮件ID,那么我会得到以下回复.

$client->setSubject('xyz-review-service@xyz-review-api.iam.gserviceaccount.com');
Run Code Online (Sandbox Code Playgroud)

响应:错误500 {"error":"unauthorized_client","error_description":"请求中未经授权的客户端或范围".}

如果我这样做完全错了,那么请建议如何继续这样做.谢谢.

php google-api google-api-php-client google-my-business-api

11
推荐指数
1
解决办法
2504
查看次数