Question

How can I "link" a person's youtube account to an account on my website? I am trying to get Analytics from videos, how much money they have made, etc. I know i am supposed to be using the YouTube Analytics API, but I see tons of different documentation and it gets SO confusing. Are there any PHP libraries I can use to get this data and to link the user's account to my web application? I am also confused on where I get an OAuth Key.

Here are some sites i have looked at:

1) Site One

2) Site Two

On site two, I looked at the examples, but nothing really helped me understand even how to start.

Was it helpful?

Solution

A lot of the relevant info you'll need can be found in this document:

https://developers.google.com/youtube/analytics/authentication

Basically, it outlines the following 4 steps:

1) Register your web app in the Google Cloud Console

This is needed so you can get a client secret and client ID, which your server-side PHP code will need in order to do the oAuth flow (and get the right scope to be able to query analytics data for the user that's authenticating). See here for more info on how to do this:

https://developers.google.com/youtube/analytics/registering_an_application

The most important things to do as your register your app are to turn on the YouTube Analytics API and create a new client ID for your web application.

2) When a user visits your page, you'll need some way (i.e. a login button, for example) to trigger the start of the oAuth flow. When this is triggered, you'll want to redirect the browser to this URL:

https://accounts.google.com/o/oauth2/auth?client_id=[YOUR CLIENT ID]&redirect_uri=[THE URL YOU WANT THE USER TO BE DIRECTED TO AFTER AUTHENTICATION]&scope=https://www.googleapis.com/auth/yt-analytics.readonly&response_type=code&access_type=offline

This will present them with a window asking them if they want to give permission to your app to read their analytics. Note that the client id parameter is the same that you received when you registered your app in step 1. That registration process also will require you to set the allowed redirect URIs, so here you must pass one you set in the registration.

3) The redirect URL will be requested, from step two, by Google's servers with a "code" parameter attched. So when it is requested, it should immediately do a POST to another URL (i.e. with cURL or something similar), that looks like this:

POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Content-Type: application/x-www-form-urlencoded

code=[CODE THAT CAME IN AS A GET PARAMETER] &client_id=[YOUR CLIENT ID]&client_secret=[YOUR CLIENT SECRET]&redirect_uri=[THE REGISTERED REDIRECT URI]&grant_type=authorization_code

If you do it as a POST with cURL, then the response will be a JSON packet that has an access token and a refresh token.

4) Your php page can store these both (in your DB, for example), note that the user should be treated as logged in at this point, and you can use the access token in the header of all API requests send to the analytics API.

https://developers.google.com/youtube/analytics/authentication#OAuth2_Calling_a_Google_API

IT'll expire in an hour, so with each request you should be checking its age (i.e. when you stored it in the DB, you could store the expiry time, for example), and when you're getting close you can use the refresh token to get a new access token.

https://developers.google.com/youtube/analytics/authentication#OAuth2_Refreshing_a_Token

You can now redirect them to wherever your app needs them to be to start interfacing with the API.

Seems like a lot? It can be, but once you get the paradigm down it's pretty simple. And you asked about a client for PHP, and thankfully there is one:

https://github.com/google/google-api-php-client

It's got simple handlers for the whole oAuth2 flow, and also has a YouTube analytics service object that sets the access token automatically for you as it's making its various calls.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top