Last active
June 29, 2017 13:14
-
-
Save yukirafsanjani/2bd05431ccbd9a22162de6041e43336e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The client ID is obtained from the {{ Google Cloud Console }} | |
// at {{ https://cloud.google.com/console }}. | |
// If you run this code from a server other than http://localhost, | |
// you need to register your own client ID. | |
var OAUTH2_CLIENT_ID = 'JW1-zIiaAY-mTZK1gVeL_ipV'; | |
var OAUTH2_SCOPES = [ | |
'https://www.googleapis.com/auth/youtube' | |
]; | |
// Upon loading, the Google APIs JS client automatically invokes this callback. | |
googleApiClientReady = function() { | |
gapi.auth.init(function() { | |
window.setTimeout(checkAuth, 1); | |
}); | |
} | |
// Attempt the immediate OAuth 2.0 client flow as soon as the page loads. | |
// If the currently logged-in Google Account has previously authorized | |
// the client specified as the OAUTH2_CLIENT_ID, then the authorization | |
// succeeds with no user intervention. Otherwise, it fails and the | |
// user interface that prompts for authorization needs to display. | |
function checkAuth() { | |
gapi.auth.authorize({ | |
client_id: OAUTH2_CLIENT_ID, | |
scope: OAUTH2_SCOPES, | |
immediate: true | |
}, handleAuthResult); | |
} | |
// Handle the result of a gapi.auth.authorize() call. | |
function handleAuthResult(authResult) { | |
if (authResult && !authResult.error) { | |
// Authorization was successful. Hide authorization prompts and show | |
// content that should be visible after authorization succeeds. | |
$('.pre-auth').hide(); | |
$('.post-auth').show(); | |
loadAPIClientInterfaces(); | |
} else { | |
// Make the #login-link clickable. Attempt a non-immediate OAuth 2.0 | |
// client flow. The current function is called when that flow completes. | |
$('#login-link').click(function() { | |
gapi.auth.authorize({ | |
client_id: OAUTH2_CLIENT_ID, | |
scope: OAUTH2_SCOPES, | |
immediate: false | |
}, handleAuthResult); | |
}); | |
} | |
} | |
// Load the client interfaces for the YouTube Analytics and Data APIs, which | |
// are required to use the Google APIs JS client. More info is available at | |
// https://developers.google.com/api-client-library/javascript/dev/dev_jscript#loading-the-client-library-and-the-api | |
function loadAPIClientInterfaces() { | |
gapi.client.load('youtube', 'v3', function() { | |
handleAPILoaded(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment