Skip to content

Instantly share code, notes, and snippets.

@gauravtiwari
Created November 19, 2016 11:28
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save gauravtiwari/e34a09bbfc5911c29d2be7dce04aee60 to your computer and use it in GitHub Desktop.
Use google client side sdk to authenticate a user and retreive access token.
/* global window document gapi */
import $ from 'jquery';
export default class Google {
constructor() {
$.getScript(window.GOOGLE_JS_SDK_URL, (data, textStatus) => {
if (textStatus === 'success' && gapi !== undefined) {
gapi.load('client:auth2', this.initClient);
}
});
}
initClient() {
gapi.client.init({
apiKey: window.GOOGLE_API_KEY,
clientId: window.GOOGLE_CLIENT_ID,
scope: 'https://www.googleapis.com/auth/youtube',
});
}
authenticate() {
return new Promise((resolve, reject) => {
gapi
.auth2
.getAuthInstance()
.signIn()
.then((data) => {
const expirationTime = new Date();
expirationTime.setSeconds(data.Zi.expires_in);
resolve({
access_token: data.Zi.access_token,
uid: data.El,
expires_at: expirationTime,
});
}, () => {
reject('Can not login. Please try again!');
});
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment