Problem with access token while creating Facebook

2019-04-14 05:52发布

问题:

I'm trying to create test users for my Facebook application. They announced this functionality in this blog post in November (http://developers.facebook.com/blog/post/429) and it is documented here (http://developers.facebook.com/docs/test_users/). I could not find the answer to this elsewhere...

According to the documentation, "You can create a test user associated with a particular application using the Graph API with the application access token." This links to the section "Autenticating as an Application" and describes this CURL script:

curl -F grant_type=client_credentials \
 -F client_id=your_app_id \
 -F client_secret=your_app_secret \
 https://graph.facebook.com/oauth/access_token

So far, so good. I ran this and get the following:

access_token=1182...18|nTI...r5Q

So now I want to POST this token to the graph api test user URL:

POST /1182...18/accounts/test-users?installed=true&permissions=read_stream&access_token=1182...18|nTI...r5Q  

When I do this (both using the Facebook PHP SDK and just typing it into the browser) I get:

{
    "error": {
      "type": "OAuthException",
      "message": "Invalid OAuth access token."
    }
}

So the questions are:

  • Why am I getting this error message?
  • Am I using the wrong access token (despite Facebook explicitly telling me to use this one?)
  • Do I need to parse the access token somehow?

    Thank you for your help.

  • 回答1:

    Here is some working code that will allow you to create a test user with the PHP SDK.



    回答2:

    Ensure your access token is properly urlencoded when passing back to facebook.



    回答3:

    I was getting this error until I removed some parameters from example code I saw floating around. Here is an example in python using requests that I finally managed to get working:

    # Get the access token
    resp = requests.get(
        'https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id={app_id}&client_secret={app_secret}'.format(
            app_id=APP_ID, # Your app id (found in admin console)
            app_secret=APP_SECRET  # Your app secret (found in admin console)
        )
    )
    # Parse the token
    token = resp.content.split('=')[-1]
    
    # Create the user
    data = {'access_token': str(token)}
    resp = requests.post(
        'https://graph.facebook.com/{app_id}/accounts/test-users?installed=true'.format(
            app_id=APP_ID
        ),
        data=data
    )
    print resp.content