ejci / Google-Auth-for-Titanium

Google Auth for Titanium
GNU General Public License v2.0
66 stars 28 forks source link

Get user gmail info #6

Closed kimnamcham closed 9 years ago

kimnamcham commented 9 years ago

How can i get user info like id, email of user by this scope: " https://www.googleapis.com/auth/userinfo.email " ?

SPorwal commented 9 years ago

In lib file (googleAuth.js), you can change tasks scope to this: scope : (o.scope) ? o.scope : ['https://www.googleapis.com/auth/plus.login', 'profile', 'email', 'openid'],

kimnamcham commented 9 years ago

I try to change scope but i can't find any information of user in response. Can you help me get this solution?

SPorwal commented 9 years ago

I also got the response only here: https://developers.google.com/oauthplayground/

Within app, there is nothing I got and still trying.

kimnamcham commented 9 years ago

I have got this solution. After get the tokens, we must request 1 url to google server to get information type: https://www.googleapis.com/oauth2/v1/userinfo?access_token=ACCESS_TOKEN&alt=json&v=2&key=DEVELOP_KEY

SPorwal commented 9 years ago

Hey, that worked. Thanks...

You got your problem solved?

Do you know how this url will work: https://www.googleapis.com/auth/plus/v1/people/ ?

kimnamcham commented 9 years ago

Send request to :https://www.googleapis.com/plus/v1/people/userId with param userId, method :get . You can see more at link: https://developers.google.com/+/api/latest/people/get

kimnamcham commented 9 years ago

I have written some code to get information of user after authorize:

/// in googleAuth.js var API_URL = 'https://www.googleapis.com/oauth2/v1/'; var devKey = "YOUR_DEV_KEY"; var OAUTH_URL = 'https://accounts.google.com/o/oauth2/'; function callMethod(args, callback, getToken) { var params = args.params; var method = args.method; var call = args.call;

    var paramsString = "";
    try {
        var xhr = Titanium.Network.createHTTPClient();
        if (params && method === 'GET') {
            for (var a in params) {
                paramsString += '&' + Ti.Network.encodeURIComponent(a) + '=' + Ti.Network.encodeURIComponent(params[a]);
            }
            if (_prop.accessToken !== null || _prop.accessToken !== undefined)
                var url = API_URL + call + '?access_token=' + _prop.accessToken + '&alt=json&v=2&key=' + devKey + paramsString;
            Ti.API.info('url: ' + url);
            xhr.open("GET", url);
        } else if (method === 'POST') {
            if (getToken) {
                xhr.open('POST', OAUTH_URL + call);
            } else {
                xhr.open('POST', API_URL + call + "?alt=json&v=2&access_token=" + _prop.accessToken + '&key=' + devKey);
            }
        }

        xhr.onerror = function(e) {
            Ti.API.error("GoogleService ERROR " + e.error);
            Ti.API.error("GoogleService ERROR " + e);
            if (callback) {
                callback({
                    error : e,
                    success : false
                });
            }
        };

        xhr.onload = function(_xhr) {
            if (callback != undefined) {
                callback({
                    error : null,
                    success : true,
                    data : xhr.responseText
                });
            }
        };

        if (method === "POST") {
            xhr.send(params);
        } else {
            xhr.send();
        }
    } catch(err) {
        Ti.UI.createAlertDialog({
            title : "Error",
            message : String(err),
            buttonNames : ['OK']
        }).show();
    }

};

/// add this line in return: callMethod: callMethod

///usage

    sync.addEventListener('click', function() {
    var params = {
        params : [],
        call : 'userinfo',
        method : 'GET'
    };
    googleAuth.isAuthorized(function() {
        Ti.API.info('Access Token: ' + googleAuth.getAccessToken());
        //user is authorized so do something... just dont forget to add accessToken to your requests
        googleAuth.callMethod(params, function(e) {
            Ti.API.info('info ' + JSON.stringify(e));
        }, null);
    }, function() {
        //authorize first
        googleAuth.authorize(function(e) {
            googleAuth.callMethod(params, function(e) {
                Ti.API.info('info ' + JSON.stringify(e));
            }, null);
        });
    });
});
ejci commented 9 years ago

Why whould you change scopes in lib file or something in the lib file? There is parameter in the googleAuth object for scopes

var googleAuth = new GoogleAuth({
    clientId : 'CLIENT_ID',
    clientSecret : 'CLIENT_SECRET',
    propertyName : 'googleToken',
    scope : ['https://www.googleapis.com/auth/tasks', 'https://www.googleapis.com/auth/tasks.readonly'],
    loginHint : 'someuser@gmail.com' 
});

The library is only doing authentication and provides you with access token. This token then you can use for calling Google APIs to get information that you need.

There is example app that is getting google tasks for that account.

SPorwal commented 9 years ago

Thanks @ejci and @kimnamcham, your solutions worked for me.

For Request queries, we can use this: https://developers.google.com/apis-explorer/#p/

Ashire commented 9 years ago

Thnx all for the great work. I am having problem to get userinfo. @kimnamcham I followed all the steps and the code will run without any error but at the end I wl get nothing.

ejci commented 9 years ago

Dont change the library. If you change it I cant help you because I don't know what you changes. Library provides you only the authorization and gives you access token that you can use in your other requests. Check the example app no how to get tasks/user info.

Ashire commented 9 years ago

I really do appreciate your feedback @ejci Miroslav Magda. I did not change it I only changed the scope in global Alloy.js. However, if I even run the sample app, I would not get the tasks. It only provides login feature and then opens an empty window. I am sorry for bothering you and I hav to admit am newbie to titanium. Would greatly appreciate if you could give me a bit of hint on how to use the given access token. Thnx in advance

ejci commented 9 years ago

Did you configured everything properly in google console?

Ashire commented 9 years ago

Yep. I was doing these for almost 10 days. Everything configured. Otherwise "login in" would not be possible. I would get the menu : sync or login. Then I wl click login it will redirect me google account login page and after login in, it will take me to an empty page. Btw am testing it on andriod S4 phone and In the google console I choose web. Is that a problem?

ejci commented 9 years ago

So thats the problem... You should use "installed application" Check here -> https://github.com/ejci/Google-Auth-for-Titanium/issues/4

ejci commented 9 years ago

Is it working? If not pls share your code so I can reproduce the behavior.

Ashire commented 9 years ago

I am still doing it. Sure. I wl let you know.

On 16 April 2015 at 02:59, Miroslav Magda notifications@github.com wrote:

Is it working? If not pls share your code so I can reproduce the behavior.

— Reply to this email directly or view it on GitHub https://github.com/ejci/Google-Auth-for-Titanium/issues/6#issuecomment-93532615 .