Find YouTube username using YouTube / GooglePlus API

I am new to Stack Overflow, so if something is wrong, please tell me

I set up my site with the Google+ API for logging in, people click the "Sign in to Google+" button and register.

This is all good and good, but I was wondering if I can integrate this registration process with the YouTube API, so that when a user clicks the login button, I can also collect information about YouTube.

I already configured the YouTube Analytics and Data APIs in the Google APIs console, I just need a code snippet to store the YouTube username.

Expected Behavior:

  • A user with the URL 'youtube.com/some-user' logs in with Google +
  • Google+ authentication key passed to script
  • Script connects to the YouTube API and returns the username (e.g. $ youtube_user = 'some-user')
  • My site takes this information and continues to work with the script
+4
source share
2 answers
Answer to

answers the question about obtaining OAuth 2 credentials for the request. From the point of view of real PHP code to work with, you can use this example as a starting point (which uses this client library ) and change line 40 to read

$channelsResponse = $youtube->channels->listChannels('snippet', array( 'mine' => 'true', )); 

And then you can access the channel name through $channelsResponse['items'][0]['snippet']['title'] . Please note that you can link your Google+ account and your YouTube channel, and if you do, the channel name will be equal to the display name of your Google+ account. If this is an untethered YouTube channel, then the obsolete channel username will be returned instead.

+1
source

You can do this using the YouTube API . What you need to do is add an extra area to your login button. I would recommend using the following area:

 https://www.googleapis.com/auth/youtube.readonly 

Do this because you will not manage any YouTube account simply by viewing their personal data (for example, to pull out links or paste the code for your YouTube videos).

Then you need to make the API call on YouTube using the client library. For experimentation you can use the explorer API . To view your information, authorize the request, set the part to β€œfragment” (without quotes), and then set me to true.

Download the "youtube", "v3" static class from the Google API client library , then make the call transfer snippet as part of what you want, and "mine" is set to true.

To list the current user videos, there is an example that comes with the PHP client library . The following change lists user actions:

  // Exchange the OAuth 2.0 authorization code for user credentials. $client->authenticate($code); $token = json_decode($client->getAccessToken()); ... $activities = $youtube->activities->listActivities('snippet', array( 'mine' => 'true', )); foreach ($activities['items'] as $activity) { error_log(serialize($activity)); } 
+3
source

All Articles