Verify Facebook Access Token for specific App

前端 未结 4 1172
执念已碎
执念已碎 2021-02-02 16:13

I need to verify that users on my iPhone app are actually logged in to my Facebook app. I\'m able to verify their user id by retrieving it with their Access token:

4条回答
  •  生来不讨喜
    2021-02-02 16:59

    Here is my implementation in Python 3, implementing a single Facebook Batch Request. This should get you both the APP and USER data that is tied to the user's access_token, in one call.

    #Build the batch, and urlencode it
    batchRequest = '[{"method":"GET","relative_url":"me"}, {"method":"GET","relative_url":"app"}]'
    batchRequestEncoded = urllib.parse.quote_plus(batchRequest)
    
    #Prepare the batch and access_token params
    dataParams = 'access_token=%s&batch=%s' % (fb_access_token, batchRequestEncoded)
    dataParamsEncoded = dataParams.encode()
    fbURL = 'https://graph.facebook.com/'
    
    #This will POST the request
    response = urllib.request.urlopen(fbURL, dataParamsEncoded)
    
    #Get the results
    data = response.read()
    data = json.loads(data.decode("utf-8"))
    

    There is probably a more pythonic way to implement some of the encoding calls I wrote, but I just wanted to show what exactly worked for me.

提交回复
热议问题