Question

I am building an Google App Engine App and running the following code

<!DOCTYPE html>
<html manifest="cache.appcache">
<head>
    <title>Eat Fresh</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8">
    <script src="scripts/whitelist.js" type="text/javascript"></script>
    <script src="https://apis.google.com/js/client.js?onload=init"></script>
</head>
<body>
</body>

</html>

 

function init() {
    CLIENT_ID = "my client ID"
    SCOPES = ["https://www.googleapis.com/auth/userinfo.email"]
    gapi.client.load('oauth2', 'v2', signin)
}

function signin() {
    gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: true}, userAuthed)
}

function userAuthed() {
    gapi.client.oauth2.userinfo.get().execute(function(resp) {
    checkEmail(resp)
    })
}

function checkEmail(user) {
    console.log(user)
    var validEmail = (whiteList.indexOf(user.email) !== -1)
    if (!user.code && validEmail) {
        startApp()
    } else {
        displayError(user.email)
    }
}

This is returning

Object {code: 401, message: "Invalid Credentials", data: Array[1], error: Object}

I have googled and googled and cannot find why this might be happening. The code used above was taken directly from another one of my Google App Engine projects and it works perfectly there. Also, if I recall, I got a similar message on the project that I took this code from and that was fixed by changing a setting on https://appengine.google.com/ or https://console.developers.google.com/project.

I have looked and looked and cannot find the webpage which helped me fix it the first time. My guess about the setting may be entirely incorrect, but it's the hunch I have.

Was it helpful?

Solution

The reason this was happening was because the access token from

gapi.auth.authorize({client_id: CLIENT_ID, scope: SCOPES, immediate: true}, userAuthed)

was returning undefined. This was due to the app not having permission to authorize the app to view google+ account data. Changing the the following

immediate: false

will prompt the user to give permission to the app, thus allowing for an authorized access token and for the remainder of the code to work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top