side validation for iPhone app

I'm developing iPhone application, that is based on communication with server, and I want to use Facebook authentication mechanisms.

Basically, I think it should work like this:

  • In my iPhone app, user logs in to Facebook, using his email and password.
  • User allows access to his data for related Facebook application.
  • My iPhone app receives access token, after successful log in.
  • In further communication with my server, my iPhone application should use the received Facebook access token (for example: in queries).
  • When my server receives some query from iPhone app, with access token, it should ask Facebook that this token is valid (and for who), and if yes, server should assume that user is authenticated with Facebook.
  • My question is: how the server should ask Facebook if given access token is valid? I think I should somehow check if the token is valid for my Facebook app.

    I've tried many Facebook queries to graph API, that I've found, but nothing worked as I expected. Can you provide me some example?


    Update: this answer seems insecure since it doesn't validate the token first as belonging to your app, see the comments, original answer as follows:

    I assume that you already have the access token in hand. In such a case the simplest way to validate an access token is to issue the following request

    https://graph.facebook.com/me?fields=id&access_token=@accesstoken
    

    Here replace @accesstoken with the access token you have. I will breakdown the url and will explain each.

    We are issuing a graph api request here which will return the Facebook User Id of the owner of the access token as a JSON string. The keyword 'me' represents the currently logged in user or the owner of the access token. For this request access token is a mandatory parameter.

    If the provided access token is not valid or expired Facebook will just return an error message of some sort.

    For a valid access token the result will somehow look like this

    {
       "id": "ID_VALUE"
    }
    

    Here's a two step process you can use to validate that a user access token belongs to your App:

    1) Generate an App Access token

    (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) Debug the User Access token

    (https://developers.facebook.com/docs/howtos/login/debugging-access-tokens/)

    https://graph.facebook.com/debug_token?
    input_token=INPUT_TOKEN
    &access_token=ACCESS_TOKEN
    

    Where INPUT_TOKEN is the user access token you want to verify, and ACCESS_TOKEN is your app's token that you got from step 1.

    The debug endpoint basically dumps all information about a token, so it'll respond with something like this:

    {
        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: [ ]
        }
    }
    

    If that token isn't from "your app" then it will return an error response.


    Another solution would be to use https://graph.facebook.com/app/?access_token=[user_access_token] as described by Get application id from user access token (or verify the source application for a token).

    This appears to be an undocumented feature, but returns JSON containing the id of the app the token was generated for. If the token wasn't for your app, it returns a 400.

    链接地址: http://www.djcxy.com/p/54104.html

    上一篇: Facebook访问令牌和AJAX调用

    下一篇: iPhone应用程序端验证