Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google api 无法使用Google API SDK或仅使用密钥获取Youtube统计数据(视图)_Google Api_Youtube Api_Google Api Php Client - Fatal编程技术网

Google api 无法使用Google API SDK或仅使用密钥获取Youtube统计数据(视图)

Google api 无法使用Google API SDK或仅使用密钥获取Youtube统计数据(视图),google-api,youtube-api,google-api-php-client,Google Api,Youtube Api,Google Api Php Client,我正在尝试访问我的youtube视频的浏览量。我曾经使用Google API的v2来获取Youtube统计数据。但由于某种原因,我尝试的东西都不管用 首先,我尝试使用谷歌提供的API浏览器。它起作用了。。。它生成了这个请求: 获取{YOUR_API_KEY} 所以我用我在谷歌API控制台中创建的键在curl中尝试了它,我还激活了Youtube API和Youtube API统计 但当我卷曲它时,我得到了这个错误: { "error": { "errors": [ { "dom

我正在尝试访问我的youtube视频的浏览量。我曾经使用Google API的v2来获取Youtube统计数据。但由于某种原因,我尝试的东西都不管用

首先,我尝试使用谷歌提供的API浏览器。它起作用了。。。它生成了这个请求:

获取{YOUR_API_KEY}

所以我用我在谷歌API控制台中创建的键在curl中尝试了它,我还激活了Youtube API和Youtube API统计

但当我卷曲它时,我得到了这个错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}
我不知道这意味着什么

所以现在我尝试使用谷歌SDK

我能得到这个:

{
 "access_token":"--------",
 "expires_in":3600,
 "created":1431449678
}
但是我得到了这个错误:

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "required",
    "message": "Login Required",
    "locationType": "header",
    "location": "Authorization"
   }
  ],
  "code": 401,
  "message": "Login Required"
 }
}
致命错误:未捕获的异常“谷歌服务异常” 调用GET时出现消息“”错误 : (403)中的“权限不足” /------/谷歌api php客户端/src/google/Http/REST.php:111 堆栈跟踪:#0 /--------/google api php客户端/src/google/Http/REST.php(63): Google_Http_REST::decodeHttpResponse(对象(Google_Http_请求), 对象(Google#u客户端))#1[内部函数]: Google_Http_REST::doExecute(对象(Google_客户端), 对象(Google_Http_请求))#2 /--------/google api php客户端/src/google/Task/Runner.php(172): 调用_user_func_数组(数组,数组)#3 /--------/google api php客户端/src/google/Http/REST.php(47): Google_Task_Runner->run()#4 /------/google api php客户端/src 在里面 /--/google-api-php-client/src/google/Http/REST.php 在线111

因此,这似乎是一个问题:

(403)许可不足

我试图找出是什么导致了这一切,但没有结果

同样,此请求在本页的Google explorer中有效:

以下是我用于请求Youtube视图的代码:

try {

    $key = file_get_contents($key_file_location);

    if( $key === false )
        throw new Exception('Failed to open/read p12 key file.');

    $cred = new Google_Auth_AssertionCredentials(
        $service_account_name,
        array('https://www.googleapis.com/auth/youtube.readonly', 'https://www.googleapis.com/auth/yt-analytics.readonly'),
        $key
    );

    $client->setAssertionCredentials($cred);

    // Check if the token is out of date
    if ($client->getAuth()->isAccessTokenExpired()) {
        $client->getAuth()->refreshTokenWithAssertion($cred);
    }

    // Reset the service_token in the PHP session
    echo $_SESSION['service_token'] = $client->getAccessToken();

} catch (Exception $e) {
    //getStatsMailError( 'Caught exception: ' . $e->getMessage() );
    error_log($e->getMessage());
}

//Option Paramaters
$optParams = array(
    'filters'       => 'video==6cBs8212oyI',
);

$result = $service->data_ga->get($channel_id, '2015-05-01', '2015-05-01', 'views', $optParams);

其余的工作正常,因为我可以获得访问令牌。

看起来您正在尝试使用一个服务帐户,YouTube API不支持该帐户。您必须设置服务器端或客户端oAuth流来授权作用域并获取访问令牌(以及刷新令牌,以便在需要时获取新的访问令牌):这是开始的地方。PHP客户端也应该能够支持整个oAuth流。我得到了我的客户身份证,秘密等等。。。它只给了我一个代币。