iPhone 앱의 Facebook 액세스 토큰 서버 측 유효성 검사
서버와의 통신을 기반으로하는 iPhone 응용 프로그램을 개발 중이며 Facebook 인증 메커니즘을 사용하고 싶습니다.
기본적으로 다음과 같이 작동해야한다고 생각합니다.
- 내 iPhone 앱에서 사용자는 이메일과 비밀번호를 사용하여 Facebook에 로그인합니다.
- 사용자는 관련 Facebook 응용 프로그램의 데이터에 액세스 할 수 있습니다.
- 로그인에 성공하면 내 iPhone 앱이 액세스 토큰을받습니다.
- 내 서버와의 추가 통신에서 내 iPhone 응용 프로그램은 수신 한 Facebook 액세스 토큰 (예 : 쿼리)을 사용해야합니다.
- 내 서버가 액세스 토큰으로 iPhone 앱에서 일부 쿼리를 수신하면 Facebook에이 토큰이 유효한지 (누가를 위해) 요청해야하며, 그렇다면 서버는 사용자가 Facebook으로 인증되었다고 가정해야합니다.
내 질문은 : 서버가 주어진 액세스 토큰이 유효한지 Facebook에 어떻게 요청해야합니까? 토큰이 Facebook 앱에 유효한지 어떻게 든 확인해야한다고 생각합니다.
내가 찾은 API를 그래프 화하기 위해 많은 Facebook 쿼리를 시도했지만 예상대로 작동하지 않습니다. 몇 가지 예를 들어 주시겠습니까?
업데이트 : 이 답변은 토큰이 앱에 속하는 것으로 먼저 유효성을 검사하지 않기 때문에 안전하지 않은 것 같습니다 . 댓글을 다음과 같이 원래 답변을 참조하십시오.
나는 당신이 이미 액세스 토큰을 가지고 있다고 가정합니다. 이 경우 액세스 토큰을 확인하는 가장 간단한 방법은 다음 요청을 발행하는 것입니다.
https://graph.facebook.com/me?fields=id&access_token=@accesstoken
여기서 @accesstoken을 보유한 액세스 토큰으로 바꿉니다. 나는 URL을 분석하고 각각을 설명 할 것이다.
여기서는 액세스 토큰 소유자의 Facebook 사용자 ID를 JSON 문자열로 리턴하는 그래프 API 요청을 발행합니다. 키워드 'me'는 현재 로그인 한 사용자 또는 액세스 토큰 소유자를 나타냅니다. 이 요청의 경우 액세스 토큰은 필수 매개 변수입니다.
제공된 액세스 토큰이 유효하지 않거나 만료 된 경우 Facebook은 일종의 오류 메시지를 반환합니다.
유효한 액세스 토큰의 경우 결과는 다음과 같습니다.
{
"id": "ID_VALUE"
}
다음은 사용자 액세스 토큰이 앱에 속하는지 확인하는 데 사용할 수있는 2 단계 프로세스입니다.
1) 앱 액세스 토큰 생성
( https://developers.facebook.com/docs/howtos/login/login-as-app/ )
https://graph.facebook.com/oauth/access_token?
client_id=YOUR_APP_ID
&client_secret=YOUR_APP_SECRET
&grant_type=client_credentials
2) 사용자 액세스 토큰 디버그
( https://developers.facebook.com/docs/howtos/login/debugging-access-tokens/ )
https://graph.facebook.com/debug_token?
input_token=INPUT_TOKEN
&access_token=ACCESS_TOKEN
여기서 INPUT_TOKEN은 확인하려는 사용자 액세스 토큰이고 ACCESS_TOKEN은 1 단계에서 얻은 앱의 토큰입니다.
디버그 엔드 포인트는 기본적으로 토큰에 대한 모든 정보를 덤프하므로 다음과 같이 응답합니다.
{
data: {
app_id: YOUR_APP_ID,
is_valid: true,
metadata: {
sso: "iphone-safari"
},
application: YOUR_APP_NAMESPACE,
user_id: USER_ID,
issued_at: 1366236791,
expires_at: 1371420791,
scopes: [ ]
}
}
해당 토큰이 "앱"이 아닌 경우 오류 응답을 반환합니다.
다른 해결책은 사용자 액세스 토큰에서 애플리케이션 ID 가져 오기 (또는 토큰의 소스 애플리케이션을 확인)에https://graph.facebook.com/app/?access_token=[user_access_token]
설명 된대로 사용하는 것 입니다.
이것은 문서화되지 않은 기능인 것 같지만 토큰이 생성 된 앱의 ID를 포함하는 JSON을 반환합니다. 토큰이 앱 용이 아닌 경우 400을 반환합니다.
최신 버전의 페이스 북 (2.2)에서는 다음과 같이 할 수 있습니다.
https://developers.facebook.com/docs/graph-api/reference/v2.2/debug_token
샘플 출력 :
{
"data": {
"app_id": "THE APP ID",
"application": "APP NAME",
"expires_at": 1427245200,
"is_valid": true,
"scopes": [
"public_profile",
"basic_info",
"read_stream",
"email",
"publish_actions",
"read_friendlists",
"user_birthday",
"user_hometown",
"user_location",
"user_likes",
"user_photos",
"user_videos",
"user_friends",
"user_posts"
],
"user_id": "THE USER ID"
}
}
private function facebookRequestMe($access_token)
{
include_once "facebook.php";
$facebook = new Facebook(array(
"appId" => "your_application_id",
"secret" => "your_application_secret"
));
$facebook->setAccessToken($access_token);
return $facebook->api("/me", "GET");
}
You can download the Facebook SDK for PHP from GitHub.
If a user has passed you a Facebook UID that they claim is theirs and you want to check if it's legit, this is a Python function that will verify it against their access token (an implementation of Robin Jome's answer):
def verify_facebook_id(id, access_token):
import requests
import simplejson
params = {'fields': 'id', 'access_token': access_token}
text = requests.get("https://graph.facebook.com/me", params=params).text
json = simplejson.loads(text)
response_id = json["id"]
return response_id == id
This is the only secure method to verify user token using just one request:
https://graph.facebook.com/debug_token?input_token={token-to-inspect}&access_token={app_id}|{app_secret}
Note that a sign "|" in the above URL isn't used as OR but as separator and must be there after fill the other fields.
The response will be JSON looking like that:
{
data: {
app_id: {app_id},
application: {app_name},
expires_at: {some_number},
is_valid: {true|false}
scopes: {array_of_permissions},
user_id: {user_id}
}
}
Reference: https://developers.facebook.com/docs/facebook-login/access-tokens/#apptokens (above method is mentioned at the bottom of this section)
Along with an access token Facebook also sends an "expires_in" parameter, which is an offset value. Use that to compute for when the access token will expire as an NSDate. Then when you need to do a request compare the current date with the expiration date.
Also try to inspect the status codes and response strings Facebook sends back.
'Programming' 카테고리의 다른 글
MySQL 두 개 이상의 주어진 값 중에서 최소 / 최대 선택 (0) | 2020.07.28 |
---|---|
ASP.NET 세션 열기 / 활성화 (0) | 2020.07.28 |
PHP로 작성된 코드 속도를 어떻게 측정 할 수 있습니까? (0) | 2020.07.28 |
다른 Makefile에서 Makefile을 호출하는 방법? (0) | 2020.07.28 |
Tensorflow Strides 인수 (0) | 2020.07.28 |