Created
November 19, 2016 11:28
-
-
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.
This file contains hidden or 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
/* 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