Android使用facebook api

来源:互联网 发布:淘宝买的翡翠原石 编辑:程序博客网 时间:2024/05/16 13:43

 转自:http://realgodo.iteye.com/blog/1236763

             http://realgodo.iteye.com/blog/1236908

             http://realgodo.iteye.com/blog/1240879

 

在android项目中使用facebook api,facebook是世界首屈一指的SNS社交网站,网站本身提供了与平台无关的api,本文旨在介绍在android项目使用facebook的api。


  • 迁出facebook sdk


首先要从github(源代码仓库)上牵出facebook的sdk,在windows下使用msysgit客户端工具,下载地址为点击这里 ,安装后 在git bash下执行如下命令,牵出facebook的sdk源代码。

git clone git://github.com/facebook/facebook-android-sdk.git



 

下载后的代码目录结构如下

 examples 例子目录,有三个例子工程

 facebook  sdk类库

 tests        测试工程

  • 将facebook sdk及demo导入eclipse


在eclipse中New-Android Project-create project from existing source,选择${git}/facebook-android-sdk/facebook。

新建后,点击工程-properties-Android,可以看到Is Library选项是处于选中状态的,ok,facebook,sdk导入完毕。


在eclipse中New-Android Project-create project from existing source,选择${git}/facebook-android-sdk/examples/simple。

新建后,点击工程-properties-Android-add reference 选择facebook sdk 工程,点击ok,demo项目导入ok。


挂上VPN,开启模拟器,开始我们的facebook之旅吧。

 

本文重点介绍下facebook接入的一些背景知识,本身其实与android是无关的。


 

Facebook Connect

 

facebook connect协议是OpenID与OAuth的结合,第三方站点/应用可以通过facebook connect这套协议规范便捷地接入facebook并使用facebook上大量的用户数据。

OpenID简介

OpenID背后的思想很简单,简化用户登录的过程,而不是录入一个个注册表单。详细的知识请点击这里 ,OpenID的优点主要有以下几点:


  • 更快、更简单的登录,因为消除或简化了注册过程。
  • 更好的登录过程和生命周期,因为用户从此只需使用一个ID和口令。

OAuth简介

第三方站点/应用提供编程接口的背景下,如何控制接入的安全与数据的可控正是OAuth要解决的问题。详细的知识请点击这里 ,OAuth的优点主要有以下几点:


  • 安全、保密的第3方数据访问。
  • 对于第3方访问的控制是声明性的。

Facebook Connect简介

Facebook Connect结合了OpenId与OAuth地思想,为用户提供统一登录页面,并对开发者的数据访问进行了声明与控制。Facebook Connect的优点:


  • 一键登录(android跨app有待调研)
  • 直接访问Facebook提供的API

Access Token

用户数据使用授权的整体的思路就是第三方应用从facebook那里获得未授权的token与密钥,用户由facebook引导对token进行授权,facebook返回授权后的access_token给第三方应用。描述下就是unAuth token ————request token————access token的过程。

客户端通过webkit浏览器打开https://www.facebook.com/dialog/oauth?client_id=${client}&redirect_uri=http://127.0.0.1/openFire.html&scope=email,offline_access&response_type=token

链接

client_id, 在facebook注册的app的id

redirect_url, 回调的URL

scope,访问的数据范围

response_type  请求类型


验证通过后如果是用户第一次使用,则会跳转至授权页面要求用户授权https://www.facebook.com/dialog/permissions.request?_path=permissions.request&app_id=${appid}&redirect_uri=http%3A%2F%2F127.0.0.1%2FopenFire.html&display=page&response_type=token&fbconnect=1&perms=email&from_login=1

返回的数据access_token(有效用户标示)和expires_in(token有效期)

连接facebook的业务大体如下图所示

   graph API 简介

    facebook的graph api是一套REST化的api,通过统一而稳定URL定义请访问数据对象,通过对http协议的GET,POST,DELETE三种请求进行数据操作的细分,响应JSON格式的数据包,组成了facebook的 graph api。

 

  • 首先看一个get请求的例子

https://graph.facebook.com/100000588112696,抓取User对象,格式客户端需要对response解析json格式,返回值如下。facebook-android/ios本身提供了sdk可以对json格式进行解析,其他平台需要额外写一套解析器来解析json格式。

{   "id": "100000588112696",   "name": "Jiqiang Bi",   "first_name": "Jiqiang",   "last_name": "Bi",   "link": "http://www.facebook.com/bijiqiang",   "username": "bijiqiang",   "gender": "male",   "locale": "zh_CN"}

 

 

  • 再来看一个post请求的例子

 https://graph.facebook.com/100000588112696/feed/ 创建一个post请求的http将参数传入

 

{ "caption": "realgodo",   "name": "realgodo",   "link": "http://realgodo.iteye.com",   "description": "Coming from my app",   "link": "http://www.facebook.com/bijiqiang",   "picture":"http://www.iteye.com/upload/logo/user/583286/acd59a73-6a14-3a17-a0ae-4ac30d5c43c8.png?1320570681"}

 

   返回的数据是

{"id": "100000588112696_302491803113754"    }

 

 

   graph Api能做什么

 

  •      抓取(fetch)

 

     首先调用调用https://graph.facebook.com/100000588112696?metadata=1&access_token=AAAAAAITEghMBAEZCbjrGZATmhq3YdIVb972ViUZBKe8KOdo5ZBk3SuZBjfTh0aS55snTMCA5dXaJSUmdqPiaGpXxH9u3qyb8YATyEa0kTfk5Yt2ELEMax

可以查看全部可以使用的graph api 包括必要的参数,一般就是access_token。

首先 看一组使用access_token的get型接口列表,主要是抓取数据。

Xml代码 复制代码 收藏代码
  1. <SPAN style="WHITE-SPACE: pre"> </SPAN> "home": "https://graph.facebook.com/100000588112696/home?access_token=",   
  2.         "feed": "https://graph.facebook.com/100000588112696/feed?access_token=",   
  3.         "friends": "https://graph.facebook.com/100000588112696/friends?access_token=",   
  4.         "mutualfriends": "https://graph.facebook.com/100000588112696/mutualfriends?access_token=",   
  5.         "family": "https://graph.facebook.com/100000588112696/family?access_token=",   
  6.         "payments": "https://graph.facebook.com/100000588112696/payments?access_token=",   
  7.         "activities": "https://graph.facebook.com/100000588112696/activities?access_token=",   
  8.         "interests": "https://graph.facebook.com/100000588112696/interests?access_token=",   
  9.         "music": "https://graph.facebook.com/100000588112696/music?access_token=",   
  10.         "books": "https://graph.facebook.com/100000588112696/books?access_token=",   
  11.         "movies": "https://graph.facebook.com/100000588112696/movies?access_token=",   
  12.         "television": "https://graph.facebook.com/100000588112696/television?access_token=",   
  13.         "games": "https://graph.facebook.com/100000588112696/games?access_token=",   
  14.         "adaccounts": "https://graph.facebook.com/100000588112696/adaccounts?access_token=",   
  15.         "likes": "https://graph.facebook.com/100000588112696/likes?access_token=",   
  16.         "posts": "https://graph.facebook.com/100000588112696/posts?access_token=",   
  17.         "tagged": "https://graph.facebook.com/100000588112696/tagged?access_token=",   
  18.         "statuses": "https://graph.facebook.com/100000588112696/statuses?access_token=",   
  19.         "links": "https://graph.facebook.com/100000588112696/links?access_token=",   
  20.         "notes": "https://graph.facebook.com/100000588112696/notes?access_token=",   
  21.         "photos": "https://graph.facebook.com/100000588112696/photos?access_token=",   
  22.         "albums": "https://graph.facebook.com/100000588112696/albums?access_token=",   
  23.         "events": "https://graph.facebook.com/100000588112696/events?access_token=",   
  24.         "groups": "https://graph.facebook.com/100000588112696/groups?access_token=",   
  25.         "videos": "https://graph.facebook.com/100000588112696/videos?access_token=",   
  26.         "picture": "https://graph.facebook.com/100000588112696/picture?access_token=",   
  27.         "inbox": "https://graph.facebook.com/100000588112696/inbox?access_token=",   
  28.         "outbox": "https://graph.facebook.com/100000588112696/outbox?access_token=",   
  29.         "updates": "https://graph.facebook.com/100000588112696/updates?access_token=",   
  30.         "accounts": "https://graph.facebook.com/100000588112696/accounts?access_token=",   
  31.         "checkins": "https://graph.facebook.com/100000588112696/checkins?access_token=",   
  32.         "apprequests": "https://graph.facebook.com/100000588112696/apprequests?access_token=",   
  33.         "friendlists": "https://graph.facebook.com/100000588112696/friendlists?access_token=",   
  34.         "friendrequests": "https://graph.facebook.com/100000588112696/friendrequests?access_token=",   
  35.         "permissions": "https://graph.facebook.com/100000588112696/permissions?access_token=",   
  36.         "notifications": "https://graph.facebook.com/100000588112696/notifications?access_token=",   
  37.         "scores": "https://graph.facebook.com/100000588112696/scores?access_token="   
 
  • 查询功能(search)

 

Xml代码 复制代码 收藏代码
  1. All public posts: https://graph.facebook.com/search?q=watermelon&type=post  
  2. People: https://graph.facebook.com/search?q=mark&type=user  
  3. Pages: https://graph.facebook.com/search?q=platform&type=page  
  4. Events: https://graph.facebook.com/search?q=conference&type=event  
  5. Groups: https://graph.facebook.com/search?q=programming&type=group  
  6. Places: https://graph.facebook.com/search?q=coffee&type=place&center=37.76,122.427&distance=1000  
  7. Checkins: https://graph.facebook.com/search?type=checkin  
 

 

  •    发布功能(publish)

https://graph.facebook.com/${PROFILE_ID}/feed

 

Xml代码 复制代码 收藏代码
  1. https://graph.facebook.com/${OBJECT_ID}/comments   
  2. https://graph.facebook.com/${OBJECT_ID}/likes   
  3. https://graph.facebook.com/${PROFILE_ID}/notes   
  4. https://graph.facebook.com/${PROFILE_ID}/links   
  5. https://graph.facebook.com/${PROFILE_ID}/events   
  6. https://graph.facebook.com/${EVENT_ID}/attending   
  7. https://graph.facebook.com/${EVENT_ID}/maybe   
  8. https://graph.facebook.com/${EVENT_ID}/declined   
  9. https://graph.facebook.com/${PROFILE_ID}/albums   
  10. https://graph.facebook.com/${ALBUM_ID}/photos   
  11. https://graph.facebook.com/${PROFILE_ID}/checkins  

  ${PROFILE_ID}是User.id,${ALBUM_ID}是Album.id

 

  •    删除功能

 

Xml代码 复制代码 收藏代码
  1. https://graph.facebook.com/${ID}?access_token=...   HTTP/1.1  

    ${ID}是对象的主键,规则是${User.id}_${Object.id}

 

    其他的功能类别还包括分析(Analytics),批量操作(Batch Requests)

 

 

  •   graph api class介绍

 

     Achievement  附件

     用户的附件

 

     Album 相册

     招聘相册

 

     Application应用程序

     在facebook注册的应用程序

 

     Checkin签到

 

     Comment评论

 

     Domain域名

 

     Event facebook事件

 

     FriendList 朋友列表

 

     Group 群组

 

     Insights 统计分析

     app,页面,域名的统计分析结果

 

     Link 链接

     分享的链接

 

     Message 消息

     线程内消息

 

     Note提示

 

     Page 页面

 

     Photo照片

 

     Post 涂鸦墙内容

 

     Question 问题

     用户提问

 

     QuestionOption 备选答案

     用户提问的一个备选答案

 

     Review反馈

     对app的反馈

 

     Status message 涂鸦墙消息

 

     Subscription订阅

 

     Thread 消息线程

 

     User 用户

 

     Video 视频