How to get the Facebook user id using the access token

后端 未结 8 2299
深忆病人
深忆病人 2020-11-28 02:19

I have a Facebook desktop application and am using the Graph API. I am able to get the access token, but after that is done - I don\'t know how to get the user\'s ID.

<
相关标签:
8条回答
  • 2020-11-28 03:01

    Check out this answer, which describes, how to get ID response. First, you need to create method get data:

    const https = require('https');
    getFbData = (accessToken, apiPath, callback) => {
        const options = {
            host: 'graph.facebook.com',
            port: 443,
            path: `${apiPath}access_token=${accessToken}`, // apiPath example: '/me/friends'
            method: 'GET'
        };
    
        let buffer = ''; // this buffer will be populated with the chunks of the data received from facebook
        const request = https.get(options, (result) => {
            result.setEncoding('utf8');
            result.on('data', (chunk) => {
                buffer += chunk;
            });
    
            result.on('end', () => {
                callback(buffer);
            });
        });
    
        request.on('error', (e) => {
            console.log(`error from facebook.getFbData: ${e.message}`)
        });
    
        request.end();
    }
    

    Then simply use your method whenever you want, like this:

    getFbData(access_token, '/me?fields=id&', (result) => {
          console.log(result);
    });
    
    0 讨论(0)
  • 2020-11-28 03:07

    The easiest way is

    https://graph.facebook.com/me?fields=id&access_token="xxxxx"
    

    then you will get json response which contains only userid.

    0 讨论(0)
提交回复
热议问题