2011-12-14 67 views
2

我建立一个FB的应用程序,其执行以下操作:为OAuth认证Facebook的代码参数

1)重定向初始请求到FB,为了认证/登录,如下:

https://www.facebook.com/dialog/oauth?client_id=MYAPPID&redirect_uri=http://localhost:8080/FB/servlet&scope=read_stream&response_type=code

2)在小服务程序,得到 “代码” 参数(这是signed_request):

String signedReq = request.getParameter("code"); 

// the String retrieved from the code parameter is: 
//3DaDJXq1Mlsq67GbeudlUxu7bY5Um4hSJlwzoPCHhp4.eyJpdiI6Ikc1ODNuRjZXbnhCb0hUV1FEMVNTQUEifQ._iXKxSGiNHfc-i5fRO35ny6hZ03DcLwu4bpAkslqoZk6OfxW5Uo36HwhUH2Gwm2byPh5rVp2kKCNS6EoPEZJzsqdhZ_MhuUD8WGky1dx5J-qNOUqQK9uNM4HG4ziSgFaAV8mzMGeUeRo8KSL0tcKuq 

//此参数在实际“代码”末尾包含'#_ = _',但我无法通过request.getParameter(“code”)获取它 ;这是一个java web应用程序。

+0

你确定参数的名称是“代码”,而不是“signed_request”? – 2011-12-14 19:12:14

+0

好吧,我现在明白了! – 2011-12-14 19:15:27

回答

3

Facebook API's OAuth Page

With this code in hand, you can proceed to the next step, app authentication, to gain the access token you need to make API calls. In order to authenticate your app, you must pass the authorization code and your app secret to the Graph API token endpoint - along with the exact same redirect_uri used above - at https://graph.facebook.com/oauth/access_token. The app secret is available from the Developer App and should not be shared with anyone or embedded in any code that you will distribute (you should use the client-side flow for these scenarios).复制

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

If your app is successfully authenticated and the authorization code from the user is valid, the authorization server will return the access token.

所以是的,这是OAuth的非常标准。抓住一个成功的代码,把它插入上面的URL(使用适当的client_id,client_secret和redirect_uri),你应该是现金。你会得到一个访问令牌,并从那里开始派对。

阅读Facebook API文章。这是相当丰富的。如果您对此有疑问,我很乐意提供帮助。

祝你好运:)