2017-10-26 4 views
0

에서 access_token이와 FB API를 그래프 사용자 정보를 요청,하지만 난내가 응용 프로그램 클라이언트가 제공 access_token이에서 페이스 북의 API 그래프 내 API에서 사용자 정보를 얻으려고 내 API의 OAuthException

{ 
"error": { 
    "message": "An active access token must be used to query information about the current user.", 
    "type": "OAuthException", 
    "code": 2500, 
    "fbtrace_id": "***********" 
} 
} 

여기에이 오류가 계속 a는 내 구조를 설명하는 그림입니다 :

enter image description here

이상한 것은 내가 간단한을 할 경우 : curl -i -H "Accept: application/json" -H "Content-Type: application/json" https://graph.facebook.com/v2.6/me?access_token=**ACCESS_TOKEN**&field=email

,536,

데이터에 유효한 json 응답이 있습니다.

하지만 내 API에서 물어볼 때 나는 위의 오류를 보였습니다 ... (futur에서 우리는 페이스 북 계정과 동일한 이메일을 가진 DB에 사용자가 있는지 확인하고 싶습니다. 계정 충돌)

내가 이런 식으로 작업을 수행합니다 대답은 코멘트에

var http = require("http"); 
var https = require("https"); 

/** 
* getJSON: REST get request returning JSON object(s) 
* @param options: http options object 
* @param callback: callback to pass the results JSON object(s) back 
*/ 
function getJSON(options, onResult) 
{ 
    console.log("rest::getJSON"); 

    var port = options.port == 443 ? https : http; 
    var req = port.request(options, function(res) 
    { 
     var output = ''; 
     console.log(options.host + ':' + res.statusCode); 
     res.setEncoding('utf8'); 

     res.on('data', function (chunk) { 
      output += chunk; 
     }); 

     res.on('end', function() { 
      var obj = JSON.parse(output); 
      onResult(res.statusCode, obj); 
     }); 
    }); 

    req.on('error', function(err) { 
    //  res.send('error: ' + err.message); 
    console.log(err.message + '\n'); 
    return(err); 
    }); 

    req.end(); 
}; 

/*//////////////////////////// 
//       // 
// Function /signup route // 
//       // 
*///////////////////////////// 

// router.post('/signup-fb', signup-fb) 
exports.signupFb = async (req, res) => { 
    const data = req.body; 

    console.log('login fb', data) 

    if (!data.fbToken) { 
     res.status(404); 
     res.json({success: false, message: "Fb token is blank"}); 
     res.end(); 
     return 
    } 

    var options = { 
    host: 'graph.facebook.com', 
    port: 443, 
    path: '/v2.6/me/', 
    method: 'GET', 
    body: {'access_token': data.fbToken, 
      'fields': 'id,name,email'} 
    }; 
    const tmp = await getJSON(options, function(statusCode, result) { 
    // I could work with the result html/json here. I could also just return it 
    console.log(tmp); 
    console.log("onResult: (" + statusCode + ")" + JSON.stringify(result)); 
    res.statusCode = statusCode; 
    res.send(result); 
}); 
+0

당신은'data.fbToken'을 확인하고 있습니다,하지만 당신은'data.access_token'을 사용하고 있습니다. ..? – CBroe

+0

Stackoverflow 아무 것도 변경 내 코드를 여기에 넣을 때 실수를 했어 ... – lu1her

+1

실제로'body' 내부에 넣어야하는지 확실하지 않습니다 - POST 요청에는 본문이 있지만 GET ... 지정하십시오 쿼리 문자열을 따로 추가하거나 경로에 추가하십시오. – CBroe

답변

0

답변.

내가이 같은 경로에 신체의 변수를 추가했다 API 그래프 요청하려면 다음

var options = { 
    host: 'graph.facebook.com', 
    port: 443, 
    path: '/v2.6/me/?access_token='+data.fbToken+'&fields=id,name,email', 
    method: 'GET' 
}; 
관련 문제