通过Firebase GoogleAuthProvider登录后访问YouTube频道ID [英] Access Youtube Channel ID once signed in though Firebase GoogleAuthProvider

查看:100
本文介绍了通过Firebase GoogleAuthProvider登录后访问YouTube频道ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了一个使用Google的基本Firebase身份验证应用程序.我已经通过了以下范围:

I've setup a basic Firebase authentication app which uses Google. I've passed the following scopes:

https://www.googleapis.com/auth/youtube.force-ssl

登录时,它表明它已获得管理我的Youtube帐户的权限,但我返回的响应中没有与Youtube相关的内容,例如channelId.

When logging in, it states that it is gaining permission to manage my Youtube Account, but the response I get back has nothing relevant to Youtube in it, such as a channelId.

即使对登录的帐户Youtube订阅执行简单的$ http.get请求,我也会得到以下响应:

Even when doing a simple $http.get request against the logged in accounts Youtube subscriptions I get the following response:

The request uses the <code>mine</code> parameter but is not properly authorized.

我是否需要通过Google登录,然后在登录后再次进行身份验证以访问我的Youtube帐户?

So would I need to login through Google, then authenticate again once signed in to access my Youtube account?

样品登录:

var provider = new firebase.auth.GoogleAuthProvider();
provider.addScope("https://www.googleapis.com/auth/youtube.force-ssl");

$scope.login = function () {

    Auth.$signInWithPopup(provider).then(function (result) {
        console.log(result);
        console.log("Signed in as:", result.user.uid);
    }).catch(function (error) {
        console.error("Authentication failed:", error);
    });

}

推荐答案

延迟致歉.

这是我解决此问题的方法.使用Firebase和Google作为提供程序登录时,我得到了Google给出的access_token并查询YouTubes API以获取正确的频道.

Here is how I solved this problem. When logging in using Firebase with Google as a provider, I get the access_token given by Google and query YouTubes API to get the correct channel.

下面是我的登录功能的示例:

An example of my login function is below:

        this.loginMainGoogle = function (event) {
            gapi.auth2.getAuthInstance().signIn().then(function _firebaseSignIn(googleUser) {
                var unsubscribe = $rootScope.authObj.$onAuthStateChanged(function (firebaseUser) {
                    unsubscribe();

                    // Check if we are already signed-in Firebase with the correct user.
                    if (!_isUserEqual(googleUser, firebaseUser)) {
                        // Build Firebase credential with the Google ID token.
                        console.log(googleUser.getAuthResponse());
                        var credential = firebase.auth.GoogleAuthProvider.credential(
                            googleUser.getAuthResponse().id_token);

                        // Sign in with credential from the Google user.
                        return $rootScope.authObj.$signInWithCredential(credential)
                            .then(function (result) {

                                var ytToken = googleUser.getAuthResponse().access_token;
                                localStorage.setItem('gToken', ytToken);
                                $rootScope.tokenerino = ytToken;

                                $http.get("https://www.googleapis.com/youtube/v3/channels?part=id&mine=true&access_token=" + ytToken)
                                .then(function(response) {
                                    $rootScope.myChan = response.data.items[0].id;
                                    localStorage.setItem('myChannelId', $rootScope.myChan);
                                    updateYTChannel(result.uid, response.data.items[0].id);
                                });

                                $rootScope.currentLoginStatus = true;
                                $rootScope.notification("You Have Signed In");

                                //Don't redirect them if they login via a YouTube playlist
                                if ($location.path().indexOf('playlists') !== 1) {
                                    $state.go('mymusic');
                                }

                            }, function errorCallback(error) {
                                console.log(error);
                            });
                    }

                })

            });

        }

我将用户的Channel存储在Firebase中,但是您可以根据需要将其放置在localStorage中.唯一的问题是access_token仅持续1个小时.希望这对任何人都有帮助,如果找到了更好的解决方案,请随时分享!

I store the Channel for the user in Firebase, but you can put it in localStorage if you want. The only problem is that the access_token only lasts for 1 hour. Hopefully this helps anyone and if a better solution has been found - feel free to share!

这篇关于通过Firebase GoogleAuthProvider登录后访问YouTube频道ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆