标签: google-api-php-client

无法连接到 www.googleapis.com 端口 443:网络无法访问

我正在使用这个库从 WordPress 网站获取 Youtube 的视频和直播。 https://github.com/google/google-api-php-client

代码在过去一个月工作正常,但突然开始随机出现连接失败错误(十次尝试中有两次失败,其他都正常)

这是我收到错误的代码:

function googleapis_get_live_event_widget(){

    //Youtube Videos and Live Events
    require_once( get_stylesheet_directory() . '/lib/google-api-php-client/src/Google/autoload.php' );
    $GOOGLE_DEVELOPER_KEY = get_field('cce_api_google_developer_key','option');

    $client = new Google_Client();
    $client->setDeveloperKey($GOOGLE_DEVELOPER_KEY);
    $youtube = new Google_Service_YouTube($client);

    try {
        $islive = get_field('cce_live_event','option');

        $opts = array();
        $opts['channelId'] = get_field('cce_api_youtube_channel_id','option');
        if($islive) {
            $opts['eventType'] = 'live';
        }
        $opts['type'] = 'video';
        $opts['maxResults'] = 1;
        $opts['order'] = 'date';


        $live_videos = $youtube->search->listSearch('id,snippet',$opts);

        $data = array();
        $data['live_events'] = $live_videos;
        echo Timber::compile('partials/live_event_widget.twig',$data);

    } catch (Exception $e) {
        $error = array();
        $error['message'] = $e->getMessage(); …
Run Code Online (Sandbox Code Playgroud)

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

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

Youtube Data API v3:喜欢另一个用户的评论

我想使用 Youtube Data API 来点赞评论。当我尝试喜欢自己的评论时,它可以正常工作,没有任何问题,但是当我尝试喜欢其他人的评论时,我会收到错误。

"reason": "invalidCommentMetadata",
"message": "The request metadata is invalid.",
Run Code Online (Sandbox Code Playgroud)

我正在使用 Google API PHP 客户端。

这是我用来更新的代码:

$videoComments = $youtube->comments->listComments('snippet', array(
     'id' => $comment->id
));

$videoComments[0]->snippet->viewerRating = 'like';

$videoCommentUpdateResponse = $youtube->comments->update('snippet', $videoComments[0]);
Run Code Online (Sandbox Code Playgroud)

我相信这是做到这一点的方法,因为当我查看文档时,我发现唯一影响类似的是“viewerRating”。

https://developers.google.com/youtube/v3/docs/comments#properties

snippet.viewerRating string 观看者对此评论的评分。请注意,此属性当前无法识别不喜欢评级,但此行为可能会发生变化。同时,财产价值就像观众对评论给予积极评价一样。

在所有其他情况下,该值为 none,包括用户对评论给予负面评级或未对评论进行评级。

此属性的有效值为: - 类似 - 无

我不知道我是否做错了什么,或者这是否可能。我希望有人能在这里帮助我。

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

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

如何在php中的google analytic api v4中使用不完全精确的运算符创建动态段?

我正在尝试创建一个动态细分来过滤我的交通数据.
我正在使用此示例https://developers.google.com/analytics/devguides/reporting/core/v4/samples#dimensions_and_metrics.
但我不想在Google_Service_AnalyticsReporting_SegmentDimensionFilter对象中使用'EXACT'运算符.我想使用像contains这样的东西,但我不知道使用什么词,我在文档中找不到它.这是我的代码:

    $client = new Google_Client();
    $client->setAuthConfigFile('authFile.json');
    $client->addScope(Google_Service_Analytics::ANALYTICS_READONLY);

    $analytics = new Google_Service_AnalyticsReporting($client);
    //var_dump($analytics);

    // Replace with your view ID. E.g., XXXX.
    $VIEW_ID = "XXXX";
    //$VIEW_ID = 

    // Create the DateRange object.
    $dateRange = new Google_Service_AnalyticsReporting_DateRange();
    $dateRange->setStartDate("2016-04-01");
    $dateRange->setEndDate("2016-04-30");

    // Create the Metrics object.
    $sessions = new Google_Service_AnalyticsReporting_Metric();
    $sessions->setExpression("ga:sessions");
    $sessions->setAlias("sessions");

    //Create the source dimension.
    $source = new Google_Service_AnalyticsReporting_Dimension();
    $source->setName("ga:source");

    // Create the segment dimension.
    $segmentDimensions = new Google_Service_AnalyticsReporting_Dimension();
    $segmentDimensions->setName("ga:segment");

    // Create Dimension Filter.
    $dimensionFilter = new Google_Service_AnalyticsReporting_SegmentDimensionFilter();
    $dimensionFilter->setDimensionName("ga:source");
    $dimensionFilter->setOperator("EXACT"); …
Run Code Online (Sandbox Code Playgroud)

php google-analytics-api google-api-php-client

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

未捕获的异常“Firebase\JWT\BeforeValidException”带有消息“无法处理 2016-11-03T21:37:13+0100 之前的令牌”

您好,我在尝试使用 google/apiclient 时遇到问题

致命错误:C:\xampp\htdocs\Google\vendor\firebase\php-jwt\ src\JWT.php:124 堆栈跟踪:#0 C:\xampp\htdocs\Google\vendor\google\apiclient\src\Google\AccessToken\Verify.php(100): Firebase\JWT\JWT::decode(' eyJhbGciOiJSUzI...', '-----BEGIN PUBL...', Array) #1 C:\xampp\htdocs\Google\vendor\google\apiclient\src\Google\Client.php(705): Google_AccessToken_Verify ->verifyIdToken('eyJhbGciOiJSUzI...', '474251646530-0t...') #2 C:\xampp\htdocs\Google\app\class\google_auth.php(51): Google_Client->verifyIdToken() #3 C:\xampp\htdocs\Google\app\class\google_auth.php(35): GoogleAuth->getPayLoad() #4 C:\xampp\htdocs\Google\index.php(10): GoogleAuth->checkRedirectCode() #5 {main} 在 C:\xampp\htdocs\Google\vendor\firebase\php-jwt\src\JWT.php 第 124 行中抛出

我的指数:

<?php
    require_once('app/ini.php');
    require_once('vendor/autoload.php');
    require_once('app/class/google_auth.php');


    $googleClient = new Google_Client();
    $auth = new GoogleAuth($googleClient);

    if ($auth->checkRedirectCode()) {
        header("Location: index.php");
    }

?>

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

    <?php if (!$auth->isLoggedIn()): //Verificar Inicio de Sesion ?>
        <a href="<?php echo $auth->getAuthUrl(); ?>">Inicie Sesion con Google</a>
    <?php else: //Si no …
Run Code Online (Sandbox Code Playgroud)

php google-api-php-client

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

Google Api Php 客户端 - 电子表格权限错误

我正在使用 Google PHP 客户端访问电子表格数据。

我收到这个致命错误:

致命错误:未捕获的异常 'Google_Service_Exception' 带有消息 '{ "error": { "code": 403, "message": "调用方没有权限", "errors": [ { "message": "调用方没有权限没有权限", "domain": "global", "reason": "forbidden" } ], "status": "PERMISSION_DENIED" } }

我的代码:

  $client = new Google_Client();
  $client->setApplicationName("Google spreadsheets");
  $client->setDeveloperKey("xxxxx");
  $client->setScopes(array('https://www.googleapis.com/auth/drive',    
  'https://www.googleapis.com/auth/spreadsheets.readonly',     
  'https://www.googleapis.com/auth/drive.file'));

  $service = new Google_Service_Sheets($client);

  $range = 'Class Data!A2:E';
  $response = $service->spreadsheets_values->get($sheetid, $range);
  $values = $response->getValues();

  if (count($values) == 0) {
      print "No data found.\n";
  } else {
    print "Name, Major:\n";
    foreach ($values as $row) {
     // Print columns A …
Run Code Online (Sandbox Code Playgroud)

php google-api google-sheets google-api-php-client

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

如何向 google oauth 2 添加参数

我正在尝试使用下面的代码向 google oAuth 发出授权请求

$client = new Google_Client();
$client->setClientId('qweqweqweqwe');
$client->setClientSecret('vQWsd1Geweqweqweqwe');
$client->setRedirectUri('http://localhost/test1');

$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$client->setScopes(['https://www.googleapis.com/auth/gmail.readonly']);

if (request()->has('code')) {

    $credentials = $client->authenticate(request('code'));

    dd($credentials);

}
Run Code Online (Sandbox Code Playgroud)

它的工作原理,但我的问题是:有什么方法可以在请求中添加用户 ID 并在回调中取回它吗?

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

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

Google 表格 - 设置背景颜色

我正在编写一个以前由另一位开发人员持有的应用程序。经过一定的处理后,他想用值填充 Google Sheets 文件。在他开始开发之前,他就走了,留给我的任务是了解 google-api-client-php 库。

我设法插入值(这对我来说是一大步),但我想为某些单元格添加背景颜色。我没有找到任何方法来实现这一点......

现在,这就是我插入值的方式:

class Sheet {
    public function __construct($client) {
        $this->service = new \Google_Service_Sheets($client);
    }
    public function write($line, $newValues, $startColumn)
    {
        $values = new \Google_Service_Sheets_ValueRange();
        $values->setValues([    $newValues  ]); 

        $this->service->spreadsheets_values->update($this->id, $range, $values, ['valueInputOption' => 'USER_ENTERED']);
    }
}
Run Code Online (Sandbox Code Playgroud)

我想创建一个colorLine()函数。

这是我的第一次尝试:

 public function colorLine($line, $r, $g, $b, $a = 1) {
   $myRange = [
        'sheetId' => 1,
        'startRowIndex' => $line,
        'endRowIndex' => $line,
        'startColumnIndex' => 0,
        'endColumnIndex' => 1000,
    ];

    $requests = [
        new \Google_Service_Sheets_Request([
            'addConditionalFormatRule' …
Run Code Online (Sandbox Code Playgroud)

php google-api-php-client google-sheets-api

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

Google 日历 API - PHP

我目前正在将Google Calendar API用于网络应用程序。但是,每小时都会提示我一个链接来验证快速启动访问权限。有谁知道如何解决这一问题?

细节:

  • 我创建了一个新的 Gmail ID:redu@gmail.com
  • redu@gmail.com 有关联的日历
  • 我的基于 php 的 Web 应用程序需要对日历执行以下操作:
  • 为每个注册用户创建一个新日历(作为 redu@gmail.com 的附加日历)
  • 为登录用户创建活动并将另一个注册用户添加为受邀者

我尝试过使用 OAUTH 和服务帐户,但没有成功。任何帮助是极大的赞赏。

下面是使用服务帐户的凭据创建 Google_Client 和 Srvice 对象的代码

function __construct()
    {
        Service account based client creation. 
        $this->client = new Google_Client();
        $this->client->setApplicationName("Redu");
        $this->client->setAuthConfig(CREDENTIALS_PATH);
        $this->client->setScopes([SCOPES]);
        $this->client->setSubject('redu@gmail.com');
        $this->client->setAccessType('offline');

        $this->service = new Google_Service_Calendar($this->client);
     }

Run Code Online (Sandbox Code Playgroud)

当我尝试使用 $service 对象创建日历或创建事件时,我收到一条错误消息,指出未设置域范围权限。但是,当我创建服务帐户时,我确实启用了域范围委派。

编辑:

下面是我使用服务帐户密钥创建 Google_Client 并使用客户端为 redu@gmail.com 创建新日历的代码。请注意,我与 reduservice@subtle-breaker-280602.iam.gserviceaccount.com 共享了 redu@gmail.com 的日历,并将权限设置为“管理更改和管理共享”。我收到的错误位于代码下方:

require (__DIR__.'/../../../vendor/autoload.php');
define('CREDENTIALS_PATH', __DIR__ . '/redu_service_account_credentials.json');
define('SCOPES', Google_Service_Calendar::CALENDAR);

function createNewCalendar($userName) {
    //Service account based client creation. 
    $client …
Run Code Online (Sandbox Code Playgroud)

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

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

无法将文件的所有权从服务帐户转移到 Google Drive 用户

尝试转移所有权时,出现以下错误:

"domain": "global",
"reason": "invalidSharingRequest",
"message": "Bad Request. User message: \"You can't change the owner of this item.\""
Run Code Online (Sandbox Code Playgroud)

这是我的代码:

use Google_Client; 
use Google_Service_Drive;
use Google_Service_Drive_DriveFile;
use Google_Service_Drive_Permission;

public function uploadDocument() {

 $FOLDER_ID = 'my_folder_ID_string';

 $client = new Google_Client();
 $client->setAuthConfig(base_path('service_account_credentials.json'));
 $client->setScopes(array('https://www.googleapis.com/auth/drive'));

 $service = new Google_Service_Drive($client);

 $fileMetadata = new Google_Service_Drive_DriveFile(array(
                                                     'name' => 'Test.pdf',
                                                     'parents' => [$FOLDER_ID]
                                                    ));

 $content = file_get_contents(public_path('tmp/Test.pdf'));

 $file = $service->files->create($fileMetadata, array(
                                                    'data' => $content,
                                                    'mimeType' => 'application/pdf',
                                                    'uploadType' => 'multipart',
                                                    'fields' => 'id'
                                                     ));

  // Transfer Ownership
  $newPermission = …
Run Code Online (Sandbox Code Playgroud)

google-drive-api google-api-php-client service-accounts

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

如何获取 Google Analytics GA4 帐户的所有媒体资源

如何通过 PHP 使用 Google Analytics GA4 列出所有帐户的所有属性?对于通用分析,我使用以下内容:

function initializeAnalyticsV3()
{
    $client = new Google_Client();
    $client->setApplicationName("Name");
    $client->setAuthConfig($KEY_FILE_LOCATION);
    $client->setScopes(['https://www.googleapis.com/auth/analytics.readonly']);
    $analytics = new Google_Service_Analytics($client);

    return $analytics;
}

$analyticsV3 = initializeAnalyticsV3();

try {
    $accounts = $analyticsV3->management_accountSummaries
        ->listManagementAccountSummaries();
} catch (apiServiceException $e) {
    print 'There was an Analytics API service error '
        . $e->getCode() . ':' . $e->getMessage();
} catch (apiException $e) {
    print 'There was a general API error '
        . $e->getCode() . ':' . $e->getMessage();
}

foreach ($accounts->getItems() as $account) {
    foreach ($account->getWebProperties() …
Run Code Online (Sandbox Code Playgroud)

php google-analytics google-analytics-api google-api-php-client google-analytics-4

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