0

I am new to google oauth and openid connect,lets say i need service account where users store videos in my youtube channel,i created service as given in docs.but i dont know how to get access token lets say my code like this

    $param = $this->getParameter('kernel.root_dir').'/xxxx.json';
    $client = new \Google_Client();
    $client->setClientId('xxxxxxxx');
    $client->setScopes('https://www.googleapis.com/auth/youtube');
    $client->setRedirectUri('http://localhost:8000/youtube/1/token');
    putenv('GOOGLE_APPLICATION_CREDENTIALS='.$param.'');
    $authurl = $client->createAuthUrl();
    $youtube = new \Google_Service_YouTube($client);
    file_get_contents($authurl);

when i run file_get_contents($authurl) am getting 401 error, what mistake i made here ?

Nishanth G
  • 85
  • 2
  • 11

1 Answers1

0

You may be confused on a few terms. Oauth2 prompts a user for authentication asking the user may I access your YouTube data. The developer is given a refresh token which they can use to retrieve a new access token and access the data on the users YouTube channel.

Service accounts are preauthorized. I could take a service account email address and grant it permission to access a folder on my Google drive account it could then read and write to that folder.

The YouTube API does not support Service accounts.

For YouTube you will need to authenticate your code once yourself save the refresh token then add that refresh token to your code. Then when another user wishes to upload a Video to your account you use the refresh token to get an access token and upload the file.

There is a good question here with how to use a refresh token with the php client library How to refresh token with Google API client?

$client->refreshToken($refreshToken);
$newtoken=$client->getAccessToken();
Community
  • 1
  • 1
DaImTo
  • 72,534
  • 21
  • 122
  • 346
  • thanks man , is ther any life time for refresh token, and is ther any reason why youtube not support service accounts? – Nishanth G May 09 '17 at 07:38
  • Refresh token *should* not expire but they can you will have to keep an eye on it and notify yourself if it does. If its not used in six months it will expire. other then that you should be safe. – DaImTo May 09 '17 at 07:58
  • 1
    Ok Thank You :-) – Nishanth G May 09 '17 at 07:59
  • This is just my opinion: YouTube is not a Google project originally they bought the company. The infrastructure of YouTube may not be able to support service accounts. but this is just a guess. – DaImTo May 09 '17 at 07:59
  • but if am trying to get access_token from refresh token, it again and again shows consent screen to get permission , how can i solve this? – Nishanth G May 11 '17 at 07:11
  • You should not be asking if you have a refresh token http://stackoverflow.com/questions/9241213/how-to-refresh-token-with-google-api-client – DaImTo May 11 '17 at 07:12