Facebook登陆服务器校验,权限请求以及数据获取

大致步骤是:

1、创建一个应用程序,可以获得App ID/API Key和App Secret。

2、使用URL:

https://www.facebook.com/dialog/oauth?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&scope=email,read_stream

其中YOUR_APP_ID就是你的APPID, YOUR_URL就是你应用程序的URL,Canvas程序就填Canvas的URI, scope就是请求的权限.

这个界面进去就是跟国内的微博应用一样了,是一个申请用户授权的页面,用户点击授权后,facebook会这样请求你的YOUR_URL:

http://your_url/?code=A_CODE_GENERATED_BY_SERVER

你的URL会收到一大串加密字符的Get请求.

3、拿出这一大串密钥,通过下面的URI获取到access_token

https://graph.facebook.com/oauth/access_token?client_id=YOUR_APP_ID&redirect_uri=YOUR_URL&client_secret=YOUR_APP_SECRET&code=THE_CODE_FROM_ABOVE

最后的code 就是第二步获取到的code了.请求这个URI会得到access_token,拿到这个之后,就可以在任何地方做你想做的事啦.

4、各种API:

 

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

News feed: https://graph.facebook.com/me/home?access_token=...

Profile feed (Wall): https://graph.facebook.com/me/feed?access_token=...

Likes: https://graph.facebook.com/me/likes?access_token=...

Movies: https://graph.facebook.com/me/movies?access_token=...

Music: https://graph.facebook.com/me/music?access_token=...

Books: https://graph.facebook.com/me/books?access_token=...

Notes: https://graph.facebook.com/me/notes?access_token=...

Permissions: https://graph.facebook.com/me/permissions?access_token=...

Photo Tags: https://graph.facebook.com/me/photos?access_token=...

Photo Albums: https://graph.facebook.com/me/albums?access_token=...

Video Tags: https://graph.facebook.com/me/videos?access_token=...

Video Uploads: https://graph.facebook.com/me/videos/uploaded?access_token=...

Events: https://graph.facebook.com/me/events?access_token=...

Groups: https://graph.facebook.com/me/groups?access_token=...

Checkins: https://graph.facebook.com/me/checkins?access_token=...

API手册:https://developers.facebook.com/docs/reference/api/

 

5、Facebook登陆服务器校验

 

1) Get 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

The debug endpoint responds 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: [ ]
    }
}

返回中回有状态,
app_id:你的应用ID
is_valid:是否成功
user_id:账号唯一ID
expires_at:过期时间
error:错误信息

  小插曲:说到这里,我做这里的时候遇到一个BUG。进行https://graph.facebook.com/oauth/access_token&client_id=YOU_APP_ID&client_secret=YOU_APP_SECRET&grant_type=client_credentials 出现了400 Bad Request 错误,我找了公司其他APPID和SECRET进行测试都是OK的,说明我的代码是没有问题的,多次无果的情况下我自己申请了一个APPID进行对比市场给我的APPID,最总发现由于上次市场人员给我演示后台不小心设置了一个东西导致了验证失败,其实出现这个问题一般是FACEBOOK那边权限校验失败导致的。她设置的地方如图。大家注意。


她应该是设置为YES了,应该为NO。

另外这种问题还有可能是Facebook 开发者后台设置问题,比如key hash,release key之类的等等参数。

facebook总结之前的几次经验。

1、Advanced里面的Native or desttop app 设置了为Yes

2、Key Hashes(developer key和release key)和不对(出现了2次)

3、没有发布

4、没有添加platform

猜你喜欢

转载自justdo2008.iteye.com/blog/2163516