Calling Youtube API for user Channels returns authorizationRequired

185 Views Asked by At

I am trying to call Youtube API for user Channels:

https://content-youtube.googleapis.com/youtube/v3/channels?part=snippet&part=statistics&mine=true&key=AIXXXXX

It returns the following response:

{
  "error": {
    "code": 401,
    "message": "The request uses the \u003ccode\u003emine\u003c/code\u003e parameter but is not properly authorized.",
    "errors": [
      {
        "message": "The request uses the \u003ccode\u003emine\u003c/code\u003e parameter but is not properly authorized.",
        "domain": "youtube.parameter",
        "reason": "authorizationRequired",
        "location": "mine",
        "locationType": "parameter"
      }
    ]
  }
}

I haven't been able to find a proper answer on stackoverflow or on youtube documentation to overcome this issue. Any help is greatly appreciated.

1

There are 1 best solutions below

2
Linda Lawton - DaImTo On

if you check the documentation for channels.list you will fine that it states

enter image description here

You need to be authorized by the user with that scope and send the access token as an authorization header bearer token.

I cant see how you are using php as you have not posted a full example of what you are doing.

As you can see this request requires an authorization header. With a valid access token. Your not sending one.

curl \
  'https://youtube.googleapis.com/youtube/v3/channels?key=[YOUR_API_KEY]' \
  --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
  --header 'Accept: application/json' \
  --compressed

php example

From here

<?php

/**
 * Sample PHP code for youtube.channels.list
 * See instructions for running these code samples locally:
 * https://developers.google.com/explorer-help/code-samples#php
 */

if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
  throw new Exception(sprintf('Please run "composer require google/apiclient:~2.0" in "%s"', __DIR__));
}
require_once __DIR__ . '/vendor/autoload.php';

$client = new Google_Client();
$client->setApplicationName('API code samples');
$client->setScopes([
    'https://www.googleapis.com/auth/youtube.readonly',
]);

// TODO: For this request to work, you must replace
//       "YOUR_CLIENT_SECRET_FILE.json" with a pointer to your
//       client_secret.json file. For more information, see
//       https://cloud.google.com/iam/docs/creating-managing-service-account-keys
$client->setAuthConfig('YOUR_CLIENT_SECRET_FILE.json');
$client->setAccessType('offline');

// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open this link in your browser:\n%s\n", $authUrl);
print('Enter verification code: ');
$authCode = trim(fgets(STDIN));

// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);

// Define service object for making API requests.
$service = new Google_Service_YouTube($client);

$response = $service->channels->listChannels('');
print_r($response);