首页 > 解决方案 > 请求使用mine参数但未正确授权

问题描述

我正在尝试使用我的参数调用 YouTube 频道 API,但出现以下错误。

[
  "error" => array:3 [
    "errors" => array:1 [
      0 => array:5 [
        "domain" => "youtube.parameter"
        "reason" => "authorizationRequired"
        "message" => "The request uses the <code>mine</code> parameter but is not properly authorized."
        "locationType" => "parameter"
        "location" => "mine"
      ]
    ]
    "code" => 401
    "message" => "The request uses the <code>mine</code> parameter but is not properly authorized."
  ]
]

通过下面的代码,我正在调用 youtube v3 API

$client = new \Google_Client();
    $client->setAuthConfig(app_path('google-api.json'));

    $client->setScopes([
        'https://www.googleapis.com/auth/youtube',
        'https://www.googleapis.com/auth/youtubepartner-channel-audit',
        \Google_Service_YouTube::YOUTUBE_FORCE_SSL,
    ]);
    $client->setRedirectUri('http://' . $_SERVER['HTTP_HOST'] . '/login');
    $client->setAccessType('offline');       
    $client->setIncludeGrantedScopes(true);

    $httpClient = $client->authorize();

    $customerResponse = $httpClient->get('https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&mine=true&key={API_KEY}');

    $customer = json_decode($customerResponse->getBody()->getContents(), true);

标签: phpyoutube-apiyoutube-data-api

解决方案


在访问我的 YouTube 频道后使用网页的“执行”按钮,您的请求有效:https ://developers.google.com/youtube/v3/docs/channels/list?apix_params=%7B%22part%22 %3A%5B%22snippet%2CcontentDetails%2Cstatistics%22%5D%2C%22mine%22%3Atrue%7D

mine如果您设置为更精确的参数,则true必须通过 OAuth2 而不是您的 API 密钥进行身份验证。


推荐阅读