Modifying user.profile using Accounts.onCreateUser for loginWithMeetup

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 13:41:08

问题


When a user creates an account using the the Meetup login package, I want to get a few more values such as the user's full name and URL to their profile picture then store them in user.profile. Currently, I'm checking if the service is "meetup", performing the following GET request and trying to store it in user.profile.

if (service == "meetup") {
        var accessToken = user.services.meetup.accessToken;

        var request = Meteor.http.get('https://api.meetup.com/2/profiles',{
            params : {
                access_token : accessToken
            },
            headers: {"User-Agent": "Meteor/1.0"}
        });

        if(result.error){
            throw result.error;
        }

        profile = _.pick(request.results,
            'name',
            'photo_url'
        );

        user.profile = profile;

        return user;

    }

However, I'm getting an error when I try to create an account. Note that I am able to create an account if I were to remove the code under if (service == "meetup") albeit without the fields I need so I know the problem is in here. How can I obtain the user's full name and profile picture URL and store them under user.profile?

Many thanks in advance :)


回答1:


Try changing your code to this (using only the APIs as described on http://docs.meteor.com):

if (service === "meetup") {
    var accessToken = user.services.meetup.accessToken;

    try {
        var result = HTTP.get('https://api.meetup.com/2/profiles',{
            params : {
                access_token : accessToken
            },
            headers: {"User-Agent": "Meteor/1.0"}
        });

        check(result.data, Object);

    } catch (error) {
        throw error;
    }

    var profile = _.pick(result.data,
        'name',
        'photo_url'
    );

    user.profile = profile;
    return user;
}



回答2:


I don't know much about the Meteor-specific parts of your question, but the information you want is probably in the get member API call rather than the profiles API call. You probably want to fetch https://api.meetup.com/2/member/self to get information about the currently-authenticated user.

The results of the API call will include the member's name as well as a photo object with a URL to the profile photo.




回答3:


OP here. Thank you for the other answers! I did manage to get the code to work. I tried a few different things so I didn't isolate what was causing the problem but I'm sure parts of it were that I was using Meteor.http.get instead of HTTP.get (as @sbking and @fuzzybabybunny suggested) and that I was using /2/profile/ instead of /2/member in the request (Thank you @mlc).

In the end, I decided to use an API key from an empty Meetup account I created instead of an access token like before.

Here is the code:

if (service == "meetup") {
        var meetupId = user.services.meetup.id;
        var target = 'https://api.meetup.com/2/member/' + meetupId + '?key=' + MEETUP_API_KEY + '&signed=true';

        var result = HTTP.get(target, {
            params: {
                format: 'json'
            }
        });

        var userProperties = result.data;

        options.profile = {
            'name': userProperties.name,
            'link': userProperties.link,
            'bio': userProperties.bio,
            'picture': userProperties.photo.photo_link,
            'id': meetupId
        };

        user.profile = options.profile;

        return user;

    }

The above code doesn't validate the returned data before storing it, which it will in its final form.

I hope this helps anyone with a similar question.



来源:https://stackoverflow.com/questions/27760064/modifying-user-profile-using-accounts-oncreateuser-for-loginwithmeetup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!