0

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.

Community
  • 1
  • 1
Hunter Mitchell
  • 7,063
  • 18
  • 69
  • 116

1 Answers1

1

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.

jlmcdonald
  • 13,408
  • 2
  • 54
  • 64
  • Very well thought out answer. Thanks! Also, would i have the user signup with their youtube or link their youtube to my already made accounts. – Hunter Mitchell Feb 23 '14 at 16:45
  • I've done it both ways; if they already have an account on your site you can give them an option to "link" the account by clicking on the button that starts the oauth flow and, when done, stores their gmail address in the DB with their user profile from your site ... or if they create a new user with the oauth flow then, when returning to your site, they have to set up a user account for you, too. The key is that you have their gmail address so when they do the oAuth flow you know which of your users it is! – jlmcdonald Feb 23 '14 at 19:59
  • I ask for the user's email at signup. Do i need to store their email from youtube as well? @jlmcdonald – Hunter Mitchell Feb 23 '14 at 20:30
  • Also, are there any rate limits for calling the API? I plan on having A LOT of users requesting their anylitics. – Hunter Mitchell Feb 23 '14 at 20:35
  • If you ask for their email at startup, they may provide you with one that isn't their Google account, but you'll need the google account stored as well as that's the only way to associate them properly when they log in with oAuth. There is a quota system, with different calls being charged different quotas. It isn't specifically defined, though, but instead talks about relative quotas: https://developers.google.com/youtube/analytics/v1/reports has more details. The reason it isn't fully defined is that it's still in a stage where they're monitoring how people are using the API. – jlmcdonald Feb 23 '14 at 23:50
  • Alright, the only issue is me wanting to get realtime data from the API. So, i did not know what i would be dealing with. – Hunter Mitchell Feb 24 '14 at 00:08