• Facebook API使用经验分享


    本例中没有使用Facebook API SDK,从底层展示了Facebook API的工作流程。

    给出了一个这样的例子:如何在服务端中利用Facebook API获取到用户的email和好友列表。

    1 申请Facebook API Key。

    详细步骤,参考这篇文章:http://help.webscribble.com/display/jconnector/Getting+the+Facebook+Connect+API+Key

    注意:“Connect URL”(也就是Redirect URL,Facebook登录成功后跳转到的路径)一定要设置正确。

    例如,我这里设置为http://127.0.0.1:8080/facebookapi/。Facebook登录成功后会跳转到这个地址,并在URL后附带access_token。

    2 Facebook登录链接。

    用下面的程序段描述登录链接的构成:

    APP_ID就是第一步中申请得到的Application ID。

    urllib.urlencode()是进行URL编码。

    1. def home(request):  
    2.     """Index page 
    3.     """  
    4.      
    5.     APP_ID = '179745182062082'  
    6.     REDIRECT_URI = 'http://127.0.0.1:8080/facebookapi/'  
    7.     login_link = 'https://www.facebook.com/dialog/oauth?' + urllib.urlencode({'client_id':APP_ID, 'redirect_uri':REDIRECT_URI, 'response_type''token''scope':'email'})  
    8.       
    9.     return HttpResponse('<a href="%s">Log in Facebook</a>' % login_link)  

    scope参数指定需要用户授权的权限(查看完整的权限列表),多个权限直接用逗号分隔。在本例中我们只请求用户授予可获取email的权限。

    上述代码生成的链接为:https://www.facebook.com/dialog/oauth?scope=publish_stream&redirect_uri=http%3A%2F%2F127.0.0.1%3A8080%2F&response_type=token&client_id=179745182062082

    如下图所示:

    3 用户登录、授权。

    这部分是用户在点击(或自动跳转到)上述链接后进行的操作。

     

    4 获取access_token。

    用户登录,授权完毕后,将自动跳转到如下的地址:

    http://127.0.0.1:8080/facebookapi/#access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe&expires_in=88953

    其中access_token是存取令牌,有了该参数就可以使用Facebook API获取各种信息。

    由于参数部分位于锚点符#之后,所以在服务端无法获取到该参数,这里我们需要采用一种变通的方法:

    1. def facebookapi(request):  
    2.     """Get user's email and friends list via Facebook API 
    3.     """  
    4.       
    5.     if request.GET.get('access_token'):  
    6.         return HttpResponse('<html><head><script>location = "?"+location.hash.slice(1);</script></head></html>')  
    7.     else:  
    8.         return HttpResponse('access_token=%s' % request.GET.get('access_token'))  

    通过JS获取锚点后的参数并跳转到http://127.0.0.1:8080/facebookapi/?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe&expires_in=88953

    这样在服务端就能获取到access_token参数了。

    5 获取用户email和好友列表。

    下面的API将以JSON格式返回当前用户的基本信息。

    https://graph.facebook.com/me/?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe

    返回数据格式:

    {"id":"100002677566512","name":"Peng Qi","first_name":"Peng","last_name":"Qi","link":"http:\/\/www.facebook.com\/profile.php?id=100002677566512","gender":"male","email":"redice\u0040163.com","timezone":8,"locale":"en_US","updated_time":"2011-07-15T14:07:34+0000"}

    下面的API将以JSON格式返回当前用户的好友列表。

    https://graph.facebook.com/me/friends?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe

    返回数据格式(当前只有一个名为Kim Kimmy的好友):

    {"data":[{"name":"Kim Kimmy","id":"100000805925544"}],"paging":{"next":"https:\/\/graph.facebook.com\/me\/friends?access_token=AAACjeiZB6FgIBADGnPw2QGbtZA2aZBAPGlSLZACk2S49CfDTJgcSxbjmAGIZAe3Iqm8SAhmklZBratqpxialzQlEdx9LTMmNBga8EZAfnZCbZBZAMDGntgdKHe&limit=5000&offset=5000"}}

    查看更多API的说明:http://developers.facebook.com/docs/reference/api/ 

    Official Guide:https://developers.facebook.com/docs/graph-api/using-graph-api

  • 相关阅读:
    Ibatis,Spring整合(注解方式注入)
    Amoeba搞定mysql主从读写分离
    ClickjackFilterDeny X-Frame-Options
    Clickjacking: X-Frame-Options header missing
    升级至Struts2 2.5.2
    js和java MD5加密
    Struts2 2.5.2
    Struts2 2.5.2的套路
    java8 集合流式操作
    JS 文本输入框放大镜效果
  • 原文地址:https://www.cnblogs.com/ouyangfang/p/2360946.html
Copyright © 2020-2023  润新知