Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Auth and Google Calendar

What I am trying to do is use Firebase to authenticate with Google. Then get some data from Google Calendar.

I got the first part working. I can authenticate with Google and get user name, email ect., but once I add calendar to the scope it gives me 401 with Error: invalid_scope.

Is it even possible to get that kind of data with Firebase?

HTML

<a href="#auth" id="auth">login please</a>
<a href="#auth" id="calendar">calendar</a>

JS

<script src="https://cdn.firebase.com/js/client/2.2.2/firebase.js"></script>
    <script src="js/jquery.js"></script>
    <script>
      $("#auth").click(function() {
        var ref = new Firebase("https://<my-super-cool-app>.firebaseio.com/");

        ref.authWithOAuthPopup("google", function(error, authData) {
            if (error) {
                console.log("Login Failed!", error);
                console.log(error);
            } else {
                console.log("Authenticated successfully with payload:", authData);
            }
        }, {
            "scope": "email, calendar"
        });

        return false;
      });
      $("#calendar").click(function() {
        $.getJSON('https://www.googleapis.com/calendar/v3/users/me/calendarList', function(data) {
            console.log(data);
        });
      });
    </script>
  </body>
</html>
like image 278
Teodors Avatar asked Oct 20 '22 16:10

Teodors


1 Answers

At present, Firebase only supports scopes listed here. Thus, you can't use the calendar* scopes to authenticate with Firebase auth.

The only real workaround here, to avoid authenticating separately against Google and Firebase, would be to manually authenticate against Google with the requested scopes, then pass that OAuth token into Firebase's authWithOAuthToken method.

In other words, reverse the auth process to log in with Google and then re-use the token in Firebase.

like image 110
Kato Avatar answered Oct 27 '22 11:10

Kato