2011-12-14 37 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文章。這是相當豐富的。如果您對此有疑問,我很樂意提供幫助。

祝你好運:)