0

My web using the PHP Google_Client to insert the youtube playlistItem in my playlist (2-leg-oauth), and get the error Client is unauthorized to retrieve access tokens using this method. Where can I setting the server using my-account@gmail.com to have the same permission to access google api such like insert playlistItem, or is there having another way without using my-account@gmail.com to get same permission?

PHP Code:

putenv('GOOGLE_APPLICATION_CREDENTIALS=client_secret.json');
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->addScope('https://www.googleapis.com/auth/youtube');
$client->setSubject('my-account@gmail.com');
Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
Fan
  • 1,124
  • 3
  • 17
  • 35

2 Answers2

1

If you have delegated domain-wide access to the service account and you want to impersonate a user account, specify the email address of the user account use

$client->setSubject($user_to_impersonate);

The YouTube API doesn't support delegated domain-wide access or service accounts. You will need to authenticate with Oauth2.

see PHP Code Samples

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449
0

You're hitting a common problem. Many developers think that a Service Account is the correct way for server apps to communicate to Google's API services for a given Google account. It isn't! A Service Account, as its name implies, is a brand new account (actually a kind of half-account since you can't actually log in to it) that is dedicated to your app.

What you need to do is a one-time procedure to generate a Refresh Token, which you will store securely. Thereafter, whenever you need to access a Google API such as YouTube, you will use that Refresh Token to generate an Access Token, which you will include in each API request. The steps are enumerated How do I authorise an app (web or installed) without user intervention? (canonical ?) and https://www.youtube.com/watch?v=hfWe1gPCnzc

Community
  • 1
  • 1
pinoyyid
  • 21,499
  • 14
  • 64
  • 115