标签: google-api-php-client

Google-api-php Refresh Token返回invalid_grant

我几乎搜索了谷歌第一页的每一个结果.但似乎无法找到答案.我正在使用Google API的refresh_token并接收:

Error refreshing the OAuth2 token, message: '{ "error" : "invalid_grant" }
Run Code Online (Sandbox Code Playgroud)

我在做什么 首先:我正在创建并存储与google api的持久连接:

$client = new Google_Client();
$client->setClientId('xxxxxx-s73q0pa41aq3i2kcpatmpas6e6kkp99h.apps.googleusercontent.com');
$client->setClientSecret('xxxxxxxxxxxx');
$client->setRedirectUri('http://xxxxxxxx/generaterefreshtoken.php');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));
$client->setAccessType('offline');


if (isset($_GET['code'])) {
  $client->authenticate();
  $_SESSION['token'] = $client->getAccessToken();
  $redirect = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'];
  header('Location: ' . filter_var($redirect, FILTER_SANITIZE_URL));
}

if (isset($_SESSION['token'])) {
 $client->setAccessToken($_SESSION['token']);
}

if (isset($_REQUEST['logout'])) {
  unset($_SESSION['token']);
  $client->revokeToken();
}

if ($client->getAccessToken()) {

    $jsonarray = json_decode($client->getAccessToken());
    $arrGoogleAuth['access_token']=$jsonarray->access_token;
    $arrGoogleAuth['refresh_token']=$jsonarray->refresh_token;
    //filewrite

    $myFile = "refreshtoken.conf";
    $fh = fopen($myFile, 'w') or die("can't open file");
    fwrite($fh, $client->getAccessToken());
    fclose($fh);


    /* …
Run Code Online (Sandbox Code Playgroud)

php google-api google-api-php-client

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

使用带有php脚本的google驱动器api自动刷新令牌

我再次跟踪本教程,使用php直接从我的REMOTE SERVER上传Google文件:所以我从Google API控制台创建了新的API项目,启用了Drive API服务,请求了OAuth客户端ID和客户端密钥,并在脚本,然后将其与Google API Client Library for PHP文件夹一起上传到此http://www.MYSERVER.com/script1.php,以检索验证码:

<?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$drive = new Google_Client();

$drive->setClientId('XXX'); // HERE I WRITE MY Client ID

$drive->setClientSecret('XXX'); // HERE I WRITE MY Client Secret

$drive->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');

$drive->setScopes(array('https://www.googleapis.com/auth/drive'));

$gdrive = new Google_DriveService($drive);

$url = $drive->createAuthUrl();
$authorizationCode = trim(fgets(STDIN));

$token = $drive->authenticate($authorizationCode);

?>
Run Code Online (Sandbox Code Playgroud)

当我访问http://www.MYSERVER.com/script1.php时,允许授权并获取我可以在第二个脚本中编写的Auth代码.然后我将其上传到http://www.MYSERVER.com/script2.php,看起来像:

<?php

require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$drive = new Google_Client();

$drive->setClientId('X');  // HERE I WRITE MY Client ID …
Run Code Online (Sandbox Code Playgroud)

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

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

PHP - 无法捕获Google API lib抛出的异常

我想捕获Google API PHP库引发的异常,但由于某种原因,它会在到达catch块之前生成" 致命错误:未捕获的异常 ".

在我的应用程序中,我有这样的事情:

try {
    $google_client->authenticate($auth_code);
} catch (Exception $e) {
    // do something
}
Run Code Online (Sandbox Code Playgroud)

这是Google_Client的authenticate():

public function authenticate($code)
{
    $this->authenticated = true;
    return $this->getAuth()->authenticate($code);
}
Run Code Online (Sandbox Code Playgroud)

authenticate($code)上面是Google_Auth_OAuth2 :: authenticate()的,其中在某一点抛出异常:

throw new Google_Auth_Exception(
    sprintf(
        "Error fetching OAuth2 access token, message: '%s'",
        $decodedResponse
    ),
    $response->getResponseHttpCode()
);
Run Code Online (Sandbox Code Playgroud)

如果我在Google_Client的身份验证中放置一个try/catch块,它会捕获异常,但如果没有它,程序就会死掉而不是从我的应用程序到达主try/catch块.

据我所知,这不应该发生.有任何想法吗?

php exception-handling google-api-php-client

8
推荐指数
1
解决办法
4982
查看次数

Google Calendar API ACL规则未向非Google电子邮件地址发送邀请

我们正在寻找有关通过Google Calendar v3 API共享Google日历帐户访问权限的一些特定专业知识.创建日历后,我们将创建ACL规则以允许其他电子邮件地址访问.我们的问题在于将自动生成的邀请发送到非Google电子邮件地址.与自动将日历添加到Gmail地址日历的Gmail地址不同,我们发现,如果以前未将电子邮件地址与Google帐户关联,则该地址不会自动从Google收到Google的邀请.如果地址是通过Web浏览器界面手动共享的话.我们可以通过从日历的共享列表中手动删除API创建的ACL规则手动删除邀请,然后再次手动添加电子邮件地址,提示发送邀请.当然,我们希望在API添加ACL规则时自动发送邀请.

请注意,这与发送事件通知无关.这是用于访问以查看日历本身.

是否有任何具有相关经验的人可以启发我们这种行为?

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

8
推荐指数
0
解决办法
363
查看次数

使用服务帐户插入Google日历条目

我正在尝试使用服务帐户在Google日历上创建条目.我真的很接近这一点,但最后一行不起作用.500 Internal Service Error当我让这个运行时,我得到了一个.否则,程序运行无错误,无论价值多少.

Calendar.php文件的内容可以发现在这里.在insert()我试图调用方法开始在该文件上的1455线.

<?php

function calendarize ($title, $desc, $ev_date, $cal_id) {

    session_start();

    /************************************************
    Make an API request authenticated with a service
    account.
    ************************************************/
    set_include_path( '../google-api-php-client/src/');

    require_once 'Google/Client.php';
    require_once 'Google/Service/Calendar.php';

    // (not real keys)
    $client_id = '843319906820-jarm3f5ctbtjj9b7lp5qdcqal54p1he6.apps.googleusercontent.com';
    $service_account_name = '843319906820-jarm3f5ctbtjj7b7lp5qdcqal54p1he6@developer.gserviceaccount.com';
    $key_file_location = '../google-api-php-client/calendar-249226a7a27a.p12';

//    echo pageHeader("Service Account Access");
    if (!strlen($service_account_name) || !strlen($key_file_location))
        echo missingServiceAccountDetailsWarning();

    $client = new Google_Client();
    $client->setApplicationName("xxxx Add Google Calendar Entries");

    if (isset($_SESSION['service_token'])) {
        $client->setAccessToken($_SESSION['service_token']);
    }

    $key = file_get_contents($key_file_location); …
Run Code Online (Sandbox Code Playgroud)

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

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

确定YouTube视频是否为宽屏?

我想肯定地知道YouTube视频是否是宽屏或不使用v3 API.有许多旧视频的比例为4:3,因此我需要检测到这一点.

这可以通过API v2实现,但现在正式退役.以下是API v3文档.

API调用看起来像这样:

https://www.googleapis.com/youtube/v3/videos?id=[VIDEOID]&part=snippet&key=[DEVELOPERKEY]
Run Code Online (Sandbox Code Playgroud)

此外,缩略图数据始终返回4:3的尺寸,因此这没有帮助.这是一个例子:

[thumbnails] => Array
(
    [default] => Array
    (
        [url] => https://i.ytimg.com/vi/nnnnnnnnn/default.jpg
        [width] => 120
        [height] => 90
    )
    ...
)
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

(我目前黑客通过在缩略图那里搬弄是非4黑条分析像素这样的:3个视频会)

以下是4:3比例的视频示例:

https://www.youtube.com/watch?v=zMJ-Dl4eJu8(老武侠视频)

4:3的武术

和16:9中的一个:

https://www.youtube.com/watch?v=7O2Jqi-LhEI(一个新的锻炼视频)

锻炼视频


更新:一个有希望的建议是探索,fileDetails.videoStreams[].aspectRatio但似乎这只适用于视频所有者.否则请求fileDetails结果

请求无法访问用户评级信息.可能会发生此错误,因为请求未得到适当授权

php youtube youtube-api aspect-ratio google-api-php-client

8
推荐指数
1
解决办法
882
查看次数

如何使用Google API PHP客户端库和Youtube API V3将视频上传到YouTube?

尝试使用Google API PHP客户端(最新版本1.1.6)上传视频,但Youtube API V3中的代码无法正常工作,并且出现500内部服务器错误.

当我没有使用任何前沿测试版时,下面的代码有什么问题,我刚刚屏蔽了3个参数,否则它是从Youtube API V3复制的.

  require_once 'google-api-php-client/src/Google/Client.php';
  require_once 'google-api-php-client/src/Google/Service/YouTube.php';

  session_start();

  $OAUTH2_CLIENT_ID = 'CHANGE1-0osfh0p5h80o9ol2uqtsjq5i7r1jun.apps.googleusercontent.com';
  $OAUTH2_CLIENT_SECRET = 'CHANGE2azMpt__VdSt9';

  $client = new Google_Client();
  $client->setClientId($OAUTH2_CLIENT_ID);
  $client->setClientSecret($OAUTH2_CLIENT_SECRET);
  $client->setScopes('https://www.googleapis.com/auth/youtube');
  $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],
      FILTER_SANITIZE_URL);
  $client->setRedirectUri($redirect);

  // Define an object that will be used to make all API requests.
  $youtube = new Google_Service_YouTube($client);

  if (isset($_GET['code'])) {
    if (strval($_SESSION['state']) !== strval($_GET['state'])) {
      die('The session state did not match.');
    }

    $client->authenticate($_GET['code']);
    $_SESSION['token'] = $client->getAccessToken();
    header('Location: ' . …
Run Code Online (Sandbox Code Playgroud)

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

8
推荐指数
1
解决办法
2957
查看次数

如何在google drive v3 PHP中更新文件

我似乎无法使用以下代码更新谷歌驱动器中的文件,一切正常但文件保持不变?我正在使用v3 api.

 function updateFile($service, $fileId, $data) {
        try {
            $emptyFile = new Google_Service_Drive_DriveFile();
            $file = $service->files->get($fileId);
            $service->files->update($fileId, $emptyFile, array(
                'data' => $data,
                'mimeType' => 'text/csv',
                'uploadType' => 'multipart'
            ));
        } catch (Exception $e) {
            print "An error occurred: " . $e->getMessage();
        }
    }
Run Code Online (Sandbox Code Playgroud)

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

8
推荐指数
1
解决办法
3000
查看次数

Google登录API - 如何使用PHP登录某人?

在Google的" 集成Google登录"页面上,底部的部分向您展示了如何使用Javascript对用户进行签名:

<a href="#" onclick="signOut();">Sign out</a>
<script>
  function signOut() {
    var auth2 = gapi.auth2.getAuthInstance();
    auth2.signOut().then(function () {
      console.log('User signed out.');
    });
  }
</script>
Run Code Online (Sandbox Code Playgroud)

我一直在寻找,我找不到使用PHP这样签署用户的方法.

我确实找到了如何完全退出Google的用户,但我不希望这样.我也知道我可以删除$_SESSION包含访问代码的变量,但这仍然不完全是我想要的.

有谁知道如何使用PHP将某人从我的Google应用程序中删除?

javascript php google-api-php-client google-signin

8
推荐指数
1
解决办法
2431
查看次数

PHP将PDF上传到Google Drive API

我有用PHP创建的网站.基本上它是一个发送文件类的项目.它使用Azure中的文档存储我将调用并将其发送到Azure.现在我想发送电子邮件以及存储在Google云端硬盘中.

因此应将其存储为具有公共访问权限的驱动器.我创建了以下代码.它工作正常我不希望用户输入任何内容.

  $client->setAccessToken($_SESSION['accessToken']);
    $service = new Google_DriveService($client);
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $file = new Google_DriveFile();
    foreach ($files as $file_name) {
        $file_path = 'files/'.$file_name;
        $mime_type = finfo_file($finfo, $file_path);
        $file->setTitle($file_name);
        $file->setDescription('This is a '.$mime_type.' document');
        $file->setMimeType($mime_type);
        $service->files->insert(
            $file,
            array(
                'data' => file_get_contents($file_path),
                'mimeType' => $mime_type
            )
        );
    }
    finfo_close($finfo);
Run Code Online (Sandbox Code Playgroud)

我想使用cURL或使用API​​从Azure URL上传.邮件发送时,它会同时自动上传到驱动器.

问题更新

我有功能发送邮件这是完美的工作.我想将一个附件存储到谷歌驱动器并检索该路径到数据库的路径存储.

这一切都将基于API,无需用户交互.该文件是格式的PDF,而不是特定的字节,它根据文件的数据而不同.

问题 :

当我将文件上传到Drive原始文件名时,重命名为无标题.这是代码.

function uploadFile($credentials, $filename, $targetPath)
{

    global $GAPIS;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $GAPIS . 'upload/drive/v2/files?uploadType=media');

    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    //curl_setopt($ch, CURLOPT_POSTFIELDS, array("title" =>"newfile.txt"));
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($filename));

    curl_setopt($ch, …
Run Code Online (Sandbox Code Playgroud)

php curl google-api azure google-api-php-client

8
推荐指数
1
解决办法
1245
查看次数