Skip to content

Instantly share code, notes, and snippets.

@uruly
Last active September 2, 2018 18:03
Show Gist options
  • Save uruly/762dac308e2b90524466d4fb8c1ddbfb to your computer and use it in GitHub Desktop.
Save uruly/762dac308e2b90524466d4fb8c1ddbfb to your computer and use it in GitHub Desktop.
// Google API Login
function youtubeLogin() {
gapi.load('client:auth2', () => {
gapi.client.init({
apiKey: config.apiKey,
clientId: config.clientId,
discoveryDocs: config.discoveryDocs,
scope: config.scopes.join(' '),
}).then(() => {
gapi.client.load('youtube', 'v3', () => {
if (gapi.auth2.getAuthInstance().isSignedIn.get()) {
// サインイン済みなら
getChannelID()
var usr = gapi.auth2.getAuthInstance().currentUser.get();
var token = usr.getAuthResponse().id_token;
firebaseLogin(token);
} else {
gapi.auth2.getAuthInstance().signIn().then(() => {
// サインインする
getChannelID()
var usr = gapi.auth2.getAuthInstance().currentUser.get();
var token = usr.getAuthResponse().id_token;
firebaseLogin(token);
}).catch(error => { console.log(error) })
}
})
}).catch(function(error) {
console.log(error);
alert(error.message);
});
})
}
// Firebase Login
function firebaseLogin(token) {
var creds = firebase.auth.GoogleAuthProvider.credential(token);
firebase.auth().signInAndRetrieveDataWithCredential(creds).then((result) => {
if (result) {
let user = result.user;
console.log(user);
// ログインできたのでどっかに遷移するとか
}
}).catch (error => {
console.log(error);
})
}
// Get YouTube ChannelID
function getChannelID() {
var request = gapi.client.youtube.channels.list({
mine: true,
part: 'contentDetails'
});
request.execute(response => {
playlistId = response.result.items[0].id;
console.log(playlistId); // これがチャンネルIDだああああああああああ
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment