标签: google-api-php-client

如何使用存储的刷新令牌获取更新的访问令牌

我正在构建一个应用程序,允许管理员验证对其分析帐户的访问权限以供脱机使用,并将刷新令牌存储在数据库中.

现在,当我尝试在前端使用API​​时,它返回以下错误:

"Access Token Expired. There wan a general error : The OAuth 2.0 access token has expired, and a refresh token is not available. Refresh tokens are not returned for responses that were auto-approved."
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止生成此错误的代码:

require_once "lib/google/Google_Client.php";
require_once "lib/google/contrib/Google_AnalyticsService.php";

$_analytics = new analytics();
$_googleClient = new Google_Client();
$_googleClient->setClientId($_analytics->gaClientId);
$_googleClient->setClientSecret($_analytics->gaClientSecret);
$_googleClient->setRedirectUri($_analytics->gaRedirectUri);
$_googleClient->setScopes($_analytics->gaScope);
$_googleClient->setAccessType($_analytics->gaAccessType);

// Returns last access token from the database (this works)
$_tokenArray['access_token'] = $_analytics->dbAccessToken($_agencyId);
$_googleClient->setAccessToken(json_encode($_tokenArray));

if($_googleClient->isAccessTokenExpired()) {
    // Don't think this is required for Analytics API V3
    //$_googleClient->refreshToken($_analytics->dbRefreshToken($_agencyId)); …
Run Code Online (Sandbox Code Playgroud)

php google-analytics-api oauth-2.0 google-api-php-client

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

使用带有PHP的google驱动器api将文件上传到特定文件夹

我正在使用谷歌驱动器api上传文件,这是我的代码到目前为止,文件上传到我的根目录,我想更改到另一个文件夹的路径,

   <?php
        error_reporting(E_ALL);
        ini_set('display_errors', 1);
        require_once 'google-api-php-client/src/Google_Client.php';
        require_once 'google-api-php-client/src/contrib/Google_DriveService.php';
        $client = new Google_Client();
        // Get your credentials from the APIs Console
        $client->setClientId('***');
        $client->setClientSecret('***');
        $client->setRedirectUri('http://***');
        $client->setScopes(array('https://www.googleapis.com/auth/drive'));

        if(empty($_GET['code']))
        {
            $client->authenticate();
        }

        $myfile= "video.mp4";
        $type='video/mp4';

        $service = new Google_DriveService($client);

        // Exchange authorization code for access token
        $accessToken = $client->authenticate($_GET['code']);
        $client->setAccessToken($accessToken);


        //Insert a file

        $file = new Google_DriveFile();
        $file->setTitle('filename.mp4');
        $file->setDescription('A video');
        $file->setMimeType($type);

        $data = file_get_contents($myfile);

        $createdFile = $service->files->insert($file, array(
              'data' => $data,
              'mimeType' => $type,
            ));

        print_r($createdFile);
        echo "<br />";
Run Code Online (Sandbox Code Playgroud)

我尝试了stackoverflow的几个帖子,但没有一个对我有用我有错误,提前谢谢

php google-drive-api google-api-php-client

9
推荐指数
2
解决办法
7072
查看次数

使用google-api-php-client获取"无法解析p12文件..."错误

我一直在寻找几天,并尝试新的和旧版本的google-api-php-client以及各种其他示例,但我似乎无法绕过这个错误.下面的代码是从GitHub检索到的服务帐户示例,我从API控制台中删除了我的凭据和文件.我有一个我正在构建的不同文件,但是为了在这个问题中使用,我认为这个更简单的文件会更容易讨论.我对这两个文件都有同样的错误.

致命错误:未捕获的异常'Google_Auth_Exception',消息'无法解析p12文件.这是一个.p12文件吗?密码是否正确?OpenSSL错误:第52行的/google-api-php-client/src/Google/Signer/P12.php

我完全不知道为什么会抛出这个错误.

我已经验证"file_get_contents"实际上是获取文件的内容,我的"notasecret"密码正在被正确拉出.我希望这个最近的提交可能有所帮助,但不幸的是,它并没有为我解决这个错误.

知道这里出了什么问题吗?谢谢你的任何建议!

<?php

session_start();
include_once "templates/base.php";

set_include_path("../src/" . PATH_SEPARATOR . get_include_path());
require_once 'Google/Client.php';
require_once 'Google/Service/Books.php';


$client_id = 'xxxx.apps.googleusercontent.com';
$service_account_name = 'xxxx@developer.gserviceaccount.com';
$key_file_location = 'xxxx-privatekey.p12';

echo pageHeader("Service Account Access");
if ($client_id == 'xxxx.apps.googleusercontent.com'
    || !strlen($service_account_name)
    || !strlen($key_file_location)) {
  echo missingServiceAccountDetailsWarning();
}

$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
$service = new Google_Service_Books($client);

if (isset($_SESSION['service_token'])) {
  $client->setAccessToken($_SESSION['service_token']);
}
$key = file_get_contents($key_file_location);
$cred = new Google_Auth_AssertionCredentials(
    $service_account_name,
    array('https://www.googleapis.com/auth/books'),
    $key
);
$client->setAssertionCredentials($cred);
if($client->getAuth()->isAccessTokenExpired()) {
  $client->getAuth()->refreshTokenWithAssertion($cred);
}
$_SESSION['service_token'] = $client->getAccessToken();


$optParams = …
Run Code Online (Sandbox Code Playgroud)

google-api-php-client

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

Google API PHP客户端 - 联系人服务

经过几个小时的深度痛苦之后,我终于使用本教程(尽管基于Google Analytics),更接近Google的API PHP客户端的配置和使用.

所以,现在我终于以一种看似合法且官方的方式认证自己.我的自然想法是存在一个contrib/Google_ContactsService.php,但令我惊讶的是,它无处可寻找其他32个服务类别.

我觉得自己又回头了.我有什么方法 - 合法和正式 - 获取特定用户的联系人?(大量的教程,但都过时和hacky).

编辑:我注意到有库的更新版本可在这里,但仍然没有在被发现的"通讯录"服务...... /服务/文件夹

编辑: 到目前为止我的进展.最后一行因Google的回复而失败:401. There was an error in your request.- 我想这是因为缺乏权限(我没有要求"联系人"权限).但是,如果没有"Google_ContactsService.php",我该怎么做呢?我搞不清楚了.看代码:

<?php
session_start();

/**
 * Require the libaries
 */

    require_once 'assets/php/Google/Google_Client.php';
    require_once 'assets/php/Google/contrib/Google_AnalyticsService.php'; // from tutorial - I am supposed to get the Contacts Service, which is nowhere to find.

/**
 * Set up the Google_Client
 */

    $client = new Google_Client();
    $client->setAccessType('online'); // default: offline
    $client->setApplicationName($apiConfig['application_name']);
    $client->setClientId($apiConfig['oauth2_client_id']);
    $client->setClientSecret($apiConfig['oauth2_client_secret']);
    $client->setRedirectUri($apiConfig['oauth2_redirect_uri']);
    $client->setDeveloperKey($apiConfig['developer_key']); // …
Run Code Online (Sandbox Code Playgroud)

php api google-api google-contacts-api google-api-php-client

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

无法在codeigniter中加载google api php客户端库

当我使用APPPATH.'libraries/Google/Client.php'时,所有包含在require_once中的文件,子文件即(Auth/AssertionCredentials.php)

A PHP Error was encountered

Severity: Warning

Message: require_once(Google/Auth/AssertionCredentials.php): failed to open stream: No such file or directory

Filename: Google/Client.php

Line Number: 18
Run Code Online (Sandbox Code Playgroud)

php codeigniter google-api-php-client

9
推荐指数
2
解决办法
3345
查看次数

如何使用Laravel管理OAuth刷新令牌?

Socialiate插件为Laravel中的OAuth提供了一个实现,但它似乎主要是为了让他们不必在自己的网站上创建用户帐户.

我正在创建一个有助于管理其Youtube帐户的应用程序,这意味着auth请求的范围更广(容易更改)但我还需要刷新令牌(而不仅仅是访问令牌)以便长期访问其帐户.

是否有Laravel的包装已经处理过了?我找不到一个,但也许我正在寻找错误的东西.

如果没有,我该怎么做呢?当我编写与Youtube的API交互的代码时,我是否只需要检查访问令牌是否过期,如果是,请编写一个执行HTTP请求的函数来获取一个带有我存储在其中的刷新令牌的新请求数据库?我想还会扩展Socialite来检索刷新令牌?

我觉得必须有一个更好的方式,不要让我重新发明轮子.

oauth laravel google-api-php-client laravel-socialite laravel-5.1

9
推荐指数
2
解决办法
4689
查看次数

在Android应用上从Google云端存储上传和检索图片

我正在构建一个Android应用程序,用户需要上传和检索图像.我需要使用Google云端存储.如果有办法怎么办呢?我使用PHP作为后端和API.我看到他们为此提供了API,但没有办法直接实现PHP.后端和API的代码上传到Google Compute Engine.

php google-cloud-storage google-compute-engine google-api-php-client google-cloud-platform

9
推荐指数
1
解决办法
1058
查看次数

如何使用纤薄的3 Rest API授权google-api-php-client?

我正在尝试创建一个基于Web的电子邮件客户端,它可以从google mail API获取所有电子邮件数据.我正在使用Slim3来创建一个宁静的API接口.要访问Google API,我使用的是Google-API-PHP-Client(Google确实有其他API访问权限,我真的很喜欢它但是我仍然没有弄清楚如果不使用PHP-client-library,授权将如何工作).

我的主要问题是如何构建身份验证的一部分,因为谷歌使用Oauth2进行登录,从而提供代码.我可以在Slim中使用基于令牌的简单身份验证,但是我如何实现以下功能:

  1. 使用谷歌进行身份验证/授权.
  2. 识别新用户和返回用户.
  3. 从谷歌和本地API维护和保留访问和刷新令牌
  4. 由于API将在移动客户端和Web浏览器上使用,我不能使用PHP的默认会话 - 我依赖于数据库驱动的自定义令牌.

我如何构建API?

一种方法是使用谷歌的令牌作为应用程序中唯一的令牌 - 但它每小时都在不断变化,所以如何从令牌中识别用户 - 为每个来电呼叫谷歌API似乎不是一个优雅的解决方案.

任何线索/链接都会非常有用.

提前致谢

php authentication slim google-api-php-client

9
推荐指数
1
解决办法
1027
查看次数

Google api php客户端代码不返回刷新令牌

我一直试图从谷歌api使用谷歌客户端"代码"从谷歌客户端返回刷新令牌.它返回我发送到服务器端的代码.现在从服务器端我发送代码以使用google-api-php-client通过此调用获取刷新令牌和访问令牌:

https://www.googleapis.com/oauth2/v4/token
Run Code Online (Sandbox Code Playgroud)

虽然我使用谷歌操场上相同的代码我也得到了刷新令牌的响应,但我没有从我自己的服务器得到它..这是代码

public function getRefreshToken($code)
{
    $client = new Google_Client();
    $client->setClientId(config('services.google.client_id'));
    $client->setClientSecret(config('services.google.client_secret'));
    $client->setRedirectUri('postmessage');
    $client->setScopes(config('services.google.scopes'));
    $client->setAccessType("offline");
    $client->setApprovalPrompt("force");
    dd($client->authenticate($code));
    dd($client->getRefreshToken());
    return ;
}
Run Code Online (Sandbox Code Playgroud)

我已经将访问类型设置为离线,如某些答案中所述,但我仍然使用刷新令牌获得响应.这是响应

access_token :"xxxxxxxxxxxxxxxxxxxxxx"
created:1510242052
expires_in:3598
id_token:"xxxxxxxx"
token_type:"Bearer"
Run Code Online (Sandbox Code Playgroud)

javascript php google-authentication google-api-php-client google-oauth2

9
推荐指数
1
解决办法
502
查看次数

创建过多播放列表时出错?

我使用了YouTube API,但最近在创建播放列表时出现了新错误:

{
 "error": {
  "errors": [
   {
    "domain": "youtube.playlist",
    "reason": "exceededRateLimit",
    "message": "The user has created too many playlists recently. Please try the request again later."
   }
  ],
  "code": 403,
  "message": "The user has created too many playlists recently. Please try the request again later."
 }
}
Run Code Online (Sandbox Code Playgroud)

我通过在10分钟内创建10个播放列表来获得此消息.经过一些搜索后,似乎在10个播放列表创建后显示此消息; 与10秒或10分钟或更长时间内的时间表无关.

我们能否了解有关此新错误的更多详情?也许有时间表信息?重试后头?什么?

谢谢.

Google问题跟踪器=> https://issuetracker.google.com/issues/79222309

php youtube youtube-api google-api-php-client youtube-data-api

9
推荐指数
1
解决办法
683
查看次数