1
votes

When I try to create a playlist with the YouTube API, I get an error like below

Google_Service_Exception in REST.php line 118: { "error": { "errors": [ { "domain": "global", "reason": "insufficientPermissions", "message": "Insufficient Permission" } ], "code": 403, "message": "Insufficient Permission" } }

google callback

    $OAUTH2_CLIENT_ID = $config["api"]["google"]["client_id"];
$OAUTH2_CLIENT_SECRET = $config["api"]["google"]["client_secret"];

$client = new Google_Client();

$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');

$redirect = filter_var($config["callbacks"]["google"],
    FILTER_SANITIZE_URL);

$client->setRedirectUri($redirect);
$auth_url = $client->createAuthUrl();
$youtube = new Google_Service_YouTube($client);

$tokenSessionKey = 'token-' . $client->prepareScopes();

if ($request->get('code')) {

    if (strval($app["session"]->get('state')) !== strval($request->get('state'))) {
        die('The session state did not match.');
    }

    $client->authenticate($request->get('code'));

    $plus = new Google_Service_Plus($client);

    $field = 'iemails,name,image';

    $me = $plus->people->get('me');



    $app["session"]->set('token-session-key',$tokenSessionKey);
    $app["session"]->set($tokenSessionKey, $client->getAccessToken());
    $app['session']->set('isAuthGoogle', true);

    return $app->redirect($config["callbacks"]["main"]);
}

if ($app["session"]->get($tokenSessionKey)) {
    $client->setAccessToken($app["session"]->get($tokenSessionKey));
}

return 1;

google login

$OAUTH2_CLIENT_ID = $config["api"]["google"]["client_id"];
$OAUTH2_CLIENT_SECRET = $config["api"]["google"]["client_secret"];

$client = new Google_Client();

$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$a = $client->setScopes('https://www.googleapis.com/auth/youtube');
$b = $client->setScopes('https://www.googleapis.com/auth/plus.login');
$c = $client->setScopes('https://www.googleapis.com/auth/userinfo.email'); 

$redirect = filter_var($config["callbacks"]["google"], FILTER_SANITIZE_URL);

$client->setRedirectUri($redirect);
$auth_url = $client->createAuthUrl();

// hangi sayfadan geldik
if($_SERVER['HTTP_REFERER']){
    $app['session']->set('referer', $_SERVER['HTTP_REFERER']);
}

return $app->redirect($auth_url);

playlist insert file

$sessionKey = $app["session"]->get('token-session-key');
$accessToken = $app["session"]->get($sessionKey);
$OAUTH2_CLIENT_ID = $config["api"]["google"]["client_id"];
$OAUTH2_CLIENT_SECRET = $config["api"]["google"]["client_secret"];
$client = new Google_Client();
$client->setClientId($OAUTH2_CLIENT_ID);
$client->setClientSecret($OAUTH2_CLIENT_SECRET);
$client->setScopes('https://www.googleapis.com/auth/youtube');
$youtube = new Google_Service_YouTube($client);
$client->setAccessToken($accessToken);

$playlist = json_decode($request->get('spotifyplaylist'), true);
$playlistSnippet = new Google_Service_YouTube_PlaylistSnippet();
$playlistSnippet->setTitle($playlist["name"]);
$playlistSnippet->setDescription("Playlist created by Spotify to Youtube Playlist Conveter");
// Opsiyonel olucak
$playlistStatus = new Google_Service_YouTube_PlaylistStatus();
$playlistStatus->setPrivacyStatus('public');
$youTubePlaylist = new Google_Service_YouTube_Playlist();
$youTubePlaylist->setSnippet($playlistSnippet);
$youTubePlaylist->setStatus($playlistStatus);
$playlistResponse = $youtube->playlists->insert('snippet,status',$youTubePlaylist, array());
$playlistId = $playlistResponse['id'];

foreach ($playlist["tracks"] as $track) {
    $resourceId = new Google_Service_YouTube_ResourceId();
    $resourceId->setVideoId($track["id"]);
    $resourceId->setKind('youtube#video');
    $playlistItemSnippet = new Google_Service_YouTube_PlaylistItemSnippet();
    $playlistItemSnippet->setTitle($track["title"]);
    $playlistItemSnippet->setPlaylistId($playlistId);
    $playlistItemSnippet->setResourceId($resourceId);
    $playlistItem = new Google_Service_YouTube_PlaylistItem();
    $playlistItem->setSnippet($playlistItemSnippet);
    $playlistItemResponse = $youtube->playlistItems->insert(
        'snippet,contentDetails', $playlistItem, array());
}
1

1 Answers

0
votes

Problem Fixed

    $scope = array(
    'https://www.googleapis.com/auth/youtube.upload', 
    'https://www.googleapis.com/auth/youtube', 
    'https://www.googleapis.com/auth/youtubepartner',
    'https://www.googleapis.com/auth/plus.login',
    'https://www.googleapis.com/auth/userinfo.email'
);

$client->setScopes($scope);