【问题标题】:Facebook code parameter for OAuth authentication用于 OAuth 身份验证的 Facebook 代码参数
【发布时间】:2012-01-20 13:34:54
【问题描述】:

我构建了一个 FB 应用程序,它执行以下操作:

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)在servlet中,获取“code”参数(即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 app。

【问题讨论】:

  • 您确定参数名称是“code”而不是“signed_request”吗?

标签: java facebook oauth token


【解决方案1】:

复制自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 文章。这是非常有用的。如果您对此有任何疑问,我很乐意提供帮助。

祝你好运:)

【讨论】:

  • 我的问题不是如何继续调用图 api。//此参数在实际“代码”的末尾包含 '#_= _' 但我无法得到它通过request.getParameter("code");这是一个java web app。也就是说,如果我调用 String signedReq = request.getParameter("code");我得到的代码没有最后一个 '#_= _' 子字符串,但我可以在 url 中看到。
  • 这很奇怪...除非我弄错了,否则这意味着 FB 正在向您发送非 URL 安全代码。我假设您已经尝试在没有上述子字符串和手动附加子字符串的情况下继续?哪一个有效?如果“代码”需要子字符串才能工作,那么 FB 是否会返回一个不以该确切子字符串结尾的代码?您可能需要手动将其添加到末尾。也就是说,request.getParameter() 的 JavaDoc 没有提到它不会返回参数的整个值的任何情况。如果该值不是 URL 安全的,那么所有的赌注都将被取消......
  • 做了一点谷歌搜索。 '#' 之后的内容称为片段标识符。 HttpServletRequest 在他们的 JavaDoc 中没有提到它们。所以这很奇怪......
  • 实际上,我使用子字符串查询 FB 服务器,通过手动添加它可以工作,但如果不添加,代码验证失败。是的,正如你所说,FB 正在向我发送一个非 URL 安全代码,我不知道它是如何发生的?
猜你喜欢
  • 2011-07-07
  • 2011-09-17
  • 2011-09-09
  • 2012-05-20
  • 2016-10-14
  • 1970-01-01
  • 2022-07-07
  • 2015-02-02
  • 2011-11-27
相关资源
最近更新 更多