标签: google-api-php-client

使用PHP API恢复上传到Google云端存储

我可以使用Google PHP API CLIENT将小尺寸文件成功上传到Google云端存储,但无法上传300MB文件.返回内存错误.这是我的代码.

$storage = new Google_Service_Storage($client);

    $file_name = "filenameo.zip";

    $obj = new Google_Service_Storage_StorageObject();
    $obj->setName($file_name);

    $resp = $storage->objects->insert(
        "bucketname",
        $obj,
        array('name' => $file_name, 'data' => file_get_contents("300mb-file.zip"), 'uploadType' => 'media')
    );
Run Code Online (Sandbox Code Playgroud)

我试图将UploadType更改为可恢复..但没有运气.请帮忙.

更新:使用Http作为Brandon Yarbrough的回答

收到错误(致命错误:未捕获的异常'Google_IO_Exception')

$storage = new Google_Service_Storage($client);

$obj = new Google_Service_Storage_StorageObject();

$obj->setName("filenameo.zip");
$obj->setBucket("bucketname");


$filen = "300mb-file.zip";
$mimetype = mime_content_type($filen);


$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$status = false;



$filetoupload = array('name' => $file_name, 'data' => $filen, 'uploadType' => 'media');

$request = $storage->objects->insert("bucketname",$obj,$filetoupload); …
Run Code Online (Sandbox Code Playgroud)

php google-cloud-storage google-api-php-client resume-upload

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

为什么我在使用 Google Classroom API 时会收到 404 错误?

我正在用 PHP 编写一个应用程序,它将连接到我域的 Google Classroom。但是,当我尝试使用 Google Classroom API 执行任何操作时出现以下错误:

Message: Error calling GET https://www.googleapis.com/v1/courses?pageSize=100: (404) Not Found
Run Code Online (Sandbox Code Playgroud)

到目前为止我的代码:

$scopes = array(
  'https://www.googleapis.com/auth/classroom.courses',
  'https://www.googleapis.com/auth/classroom.courses.readonly',
  'https://www.googleapis.com/auth/classroom.rosters',
  'https://www.googleapis.com/auth/classroom.rosters.readonly'
);

$gServiceEmail = "random@developer.gserviceaccount.com";
$gServiceKey = file_get_contents("../path/to/cert.p12");

$client = new Google_Client();
$gAuth = new Google_Auth_AssertionCredentials(
  $gServiceEmail,
  $scopes,
  $gServiceKey
);

$gAuth->sub = "user@mydomain.com";
$client->setAssertionCredentials($gAuth);

$service = new Google_Service_Classroom($client);
$results = $service->courses->listCourses();
Run Code Online (Sandbox Code Playgroud)

我已在 Google 管理控制台的 API 设置中为服务帐户启用了范围,并在开发人员控制台中启用了 api。我哪里错了?

php google-api-php-client google-classroom

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

使用 gmail api 发送电子邮件时,等号丢失

我使用 gmail-api 从我的数据库向不同的联系人发送电子邮件。当我发送消息时,样式不起作用,因为发生这种情况,因为等号“=”丢失了。

例如,把这个放在消息中

<img src="mysite.com/image.jpg"/>
Run Code Online (Sandbox Code Playgroud)

但相反,我把这个放在了=

<img src"mysite.com/image.jpg"/>
Run Code Online (Sandbox Code Playgroud)

这是我为消息制作字符串的函数的一部分

    $strSubject = $data['subject'];
    //$strRawMessage = "From: myAddress<pblanco@mysite.com>\r\n"; 
    $strRawMessage = "From: <".$data['from'].">\r\n"; //email consultor
    //$strRawMessage .= "To: toAddress <pblanco@mysite.com>\r\n";
    $strRawMessage .= "To: <".$data['to'].">\r\n"; //email destinatario
    $strRawMessage .= 'Subject: =?utf-8?B?' . base64_encode($strSubject) . "?=\r\n"; //asunto
    $strRawMessage .= "MIME-Version: 1.0\r\n";
    $strRawMessage .= "Content-Type: text/html; charset=utf-8\r\n";
    $strRawMessage .= 'Content-Transfer-Encoding: quoted-printable' . "\r\n\r\n";
    $strRawMessage .= $data['message']."\r\n"; //mensaje
    // The message needs to be encoded in Base64URL
    $msg = $this->createMessage($strRawMessage);
Run Code Online (Sandbox Code Playgroud)

在这里我对消息进行编码

public function createMessage($string){
    //$mime = …
Run Code Online (Sandbox Code Playgroud)

php encoding google-api google-api-php-client gmail-api

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

通过 API 将新成员添加到我们的 Google 管理目录组

哇,我到处搜索,找不到这个问题的“好”答案。这让我相信这很容易,没有人问,我很愚蠢,或者没有人能弄清楚。

当用户加入我们的网站时,我们希望在新用户注入过程中也将他们添加到我们的谷歌群组页面。我这里有这段代码,用于列出用户(尝试这样做只是为了让我开始)

$start = microtime(true);
require_once __DIR__ . '/google-api-php-client-2.2.1/vendor/autoload.php';
session_start();
date_default_timezone_set('America/Los_Angeles');
$REDIRECT_URI = 'http://localhost:8080';
$KEY_LOCATION = __DIR__ . '/client_secret.json';
$TOKEN_FILE   = "token.txt";
$SCOPES = array(
  Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER
);

$client = new Google_Client();
$client->setApplicationName("Testing stuff");
$client->setAuthConfig($KEY_LOCATION);

// Incremental authorization
$client->setIncludeGrantedScopes(true);
$client->setAccessType('offline');
$client->setRedirectUri($REDIRECT_URI);
$client->setScopes($SCOPES);
$client->setAccessToken($_SESSION['accessToken']);
Run Code Online (Sandbox Code Playgroud)

这就是我所得到的,请怜悯我。

*** 更新 ***

我现在已经走到这一步了,感觉自己更接近了。

<?php

require_once('google-api-php-client-2.2.1_PHP54/vendor/autoload.php');
$impersonateUser = 'XXX';
define('SCOPES', implode(' ', array( 
Google_Service_Directory::ADMIN_DIRECTORY_GROUP_MEMBER) ));
putenv('GOOGLE_APPLICATION_CREDENTIALS=' . 'c_json' );

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($impersonateUser);
$client->setScopes(SCOPES);

$dir = new Google_Service_Directory_Member($client);

$gPrimaryEmail = 'bob@bob.com';

$newUser = array( "email" …
Run Code Online (Sandbox Code Playgroud)

php google-api google-api-php-client google-admin-sdk

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

在 google Drive api 的根目录中创建文件夹并将文件上传到该文件夹

我想使用 CURL 在 googledrive 根目录中创建文件夹。文件已上传到驱动器,但我需要创建一个文件夹并将文件上传到该文件夹​​。

根据 @hanshenrik 代码 创建文件夹正在工作 移动文件不起作用

我更新的代码:

$REDIRECT_URI = 'http' . ($_SERVER['SERVER_PORT'] == 80 ? '' : 's') . '://' . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'];
$SCOPES = array($GAPIS_AUTH . 'drive', $GAPIS_AUTH . 'drive.file', $GAPIS_AUTH . 'userinfo.email', $GAPIS_AUTH . 'userinfo.profile');
$STORE_PATH = 'credentials.json';

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

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

   //$content =  { title  "mypdf.pdf", description = "mypdf.pdf", mimeType = "application/pdf" };

   $contentArry = array('name' =>'veridoc', 'parents' => array('17dVe2GYpaHYFdFn1by5-TYKU1LXSAwkp'));
   $contentArry …
Run Code Online (Sandbox Code Playgroud)

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

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

Google API PHP 客户端服务中是否有 FCM 服务

我已经浏览了 Github,但找不到任何相关的类或文档。

https://github.com/google/google-api-php-client-services/tree/master/src/Google/Service

我正在尝试从服务器向 Web 客户端发送 FCM 消息。以下是我目前如何实现这一目标。

<?php
header('Content-Type: text/json');

$data = array(
    'message' => array(
        'notification' => array(
            'title' => 'FCM Message',
            'body' => 'This is an FCM Message',
        )
    )
);

$server_key = 'ya29.ElqKBGN2Ri_Uz...HnS_uNreA';

$url = 'https://fcm.googleapis.com/fcm/send';
$headers = 'Authorization:key = '.$firebase_api_key."\r\n".'Content-Type: application/json'."\r\n".'Accept: application/json'."\r\n";

$registration_ids = array('bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...');
$fields = array('registration_ids' => $registration_ids, 'data' => $data);

$content = json_encode($fields);
$context = array('http' => array( 'method' => 'POST', 'header' => $headers, 'content' => $content));
$context = stream_context_create($context);
$response = …
Run Code Online (Sandbox Code Playgroud)

php google-api-php-client firebase-cloud-messaging

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

使用服务帐户使用 Google Drive API 创建文件不会在授权用户帐户的 GD 上呈现文件

我目前正在尝试创建一个文件并使用 Google 提供的服务帐户身份验证方法将其上传到 Google Drive 帐户(因为它在最终产品上都是服务器端的,我不希望用户拥有授权访问,所以没有 OAuth 每说)。

我目前可以连接服务帐户并上传文件,但在我浏览 Google 云端硬盘时未显示该文件。

(我能够使用 OAuth 方法让它工作(上传和渲染);我会在上传之前手动授权应用程序,所以我很确定代码正在工作)

另外,我知道它正在工作的另一种方式是,一旦上传完成,我就会返回一个文件 ID。如果我去“https://drive.google.com/file/d/FILE_ID/view?usp=sharing”,我会遇到“需要授权”。

在创建服务帐户时我应该做些什么以便我的 Gmail 帐户可以访问这些文件?


有关信息,这是我连接到 API(服务器端)的方式:pastebin.com/3HacF02F

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

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

Google 服务帐户权限/未找到文件错误

我希望使用 PHP 脚本和 Google Drive API(将设置为 cron 作业)将 MySQL 备份从网络服务器推送到 Google Drive。

我知道这是一个服务器到服务器的应用程序,因此需要 Google 服务帐户(我知道可以使用用户帐户并刷新访问令牌 - 但我认为这不是正确的用例因为我不希望有任何用户交互?)。

我已在 Cloud Console 中设置了 Google 服务帐号。使用我的常规帐户,我与服务帐户共享一个目录,并且我尝试使用以下脚本上传测试文件:

function getClient()
{
    $client = new Google_Client();

    if ($credentials_file = checkServiceAccountCredentialsFile()) 
    {
        // set the location manually
        $client->setAuthConfig($credentials_file);
    }
    else 
    {
        echo missingServiceAccountDetailsWarning();
        return;
    }

    $client->setApplicationName('Backups');
    $client->setScopes(Google_Service_Drive::DRIVE);

    $guzzleClient = new \GuzzleHttp\Client(array( 'curl' => array( CURLOPT_SSL_VERIFYPEER => false, ), ));
    $client->setHttpClient($guzzleClient);

    return $client;
}
Run Code Online (Sandbox Code Playgroud)
$file_to_upload = 'test.txt';

if(file_exists($file_to_upload))
{
    // Get the API client and construct the service …
Run Code Online (Sandbox Code Playgroud)

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

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

需要帮助使用Google API Client Library for PHP创建QPX Express请求

我想找到特价航班的最低价格,并希望使用Google Developers提供的QPX Express API.由于我是PHP开发人员,我认为用于PHPGoogle API客户端库是我的首选方法.

但是,在使用QPX Express API组合PHP API时,我感到困惑:

关于QPX Express,我知道我必须创建一个必须发送到API的JSON对象,甚至可以通过https://qpx-express-demo.itasoftware.com/上的Demo轻松完成.

关于PHP API客户端,我想我必须创建一个Client对象和QPX Express Service对象,如下所示:

require_once 'Google/Client.php';
require_once 'Google/Service/QPXExpress.php';
$client = new Google_Client();
$client->setApplicationName("Testing");
$client->setDeveloperKey("MY_APP_KEY");
$service = new Google_Service_QPXExpress($client);
Run Code Online (Sandbox Code Playgroud)

(我已经在Google Developers Console中创建了一个新项目和一个API KEY.)

但后来我不知道该怎么用它来发送JSON请求并收到JSON响应.我要么找不到正确的资源,要么我对RESTful API缺乏了解......不幸的是,我没有找到类似于特殊情况的教程(PHP API和QPX),简单的例子没有多大帮助, QPX Express参考文献也没有.所以,我希望有人能把我放在正确的轨道上...提前致谢!

更新:ämbi的第一个答案的帮助下,我想出了以下代码,导致致命错误.

代码:

require_once 'Client.php';
require_once 'Service/QPXExpress.php';
$client = new Google_Client();
$client->setApplicationName("Testing");
$client->setDeveloperKey("[myKey]");
$service = new Google_Service_QPXExpress($client);
$request = new Google_Service_QPXExpress_TripOptionsRequest();
$request->setMaxPrice('EUR200');
$searchRequest = new Google_Service_QPXExpress_TripsSearchRequest();
$searchRequest->setRequest($request);
$result = $service->trips->search($searchRequest); …
Run Code Online (Sandbox Code Playgroud)

php api json google-api-php-client

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

使用php将事件添加到Google日历

我正在开发一个客户端Web应用程序,用户可以在其中预订带有日期,时间,位置等的驱动器

客户要求将所有预订作为活动添加到他的Google日历中

我创建了一个API密钥并下载了PHP API客户端:https : //github.com/google/google-api-php-client

但是,当我尝试添加事件时,出现“需要登录”错误

我如何直接添加事件而无需使用OAuth和同意屏幕,因为该功能将在后端自动执行,因此我拥有对gmail帐户的完全访问权限。

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

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