在 Ruby 中从 Facebook Open Graph 响应获取访问令牌

发布于 2024-09-04 17:30:58 字数 839 浏览 3 评论 0原文

我正在尝试在我的 ruby​​ sinatra 应用程序中使用 facebook 实现单点登录。到目前为止,我一直在关注本教程:

http://jaywiggins。 com/2010/05/facebook-oauth-with-sinatra/

我能够向用户发送连接到我的应用程序的请求,但我在实际“获取”访问令牌时遇到困难。用户可以毫无问题地连接,并且我收到带有“code”参数的响应,我应该用它来交换访问令牌 - 但我在这里遇到了困难。

因此,我提交带有以下参数的网址:

https://graph.facebook.com/oauth/access_token /{client_id}&{client_secret}&{code}&{redirect_uri}

上面大括号中的单词显然被值替换了。

我使用以下代码提交此内容:

response = open(url)

这似乎没有以访问令牌的方式返回任何有用的内容(它有一个 @base_uri,这是我上面提交的 url 和一些其他参数,尽管看起来没什么用处)。但是,如果我获取提交的 URL 并将其粘贴到浏览器中,我会收到一个访问令牌。

谁能告诉我如何从 Facebook 获取请求并提取访问令牌?

谢谢。

I'm trying to implement single sign-on using facebook in my ruby sinatra app. So far, I've been following this tutorial:

http://jaywiggins.com/2010/05/facebook-oauth-with-sinatra/

I am able to send a request for a user to connect to my application but I'm having trouble actually "getting" the access token. The user can connect without trouble and I receive a response with the "code" parameter, which I'm supposed to use to exchange an Access Token - but its here where I get stuck.

So I submit a url with the following parameters:

https://graph.facebook.com/oauth/access_token/{client_id}&{client_secret}&{code}&{redirect_uri}

The words in the curly brackets above are obviously replaced by the values.

I submit this using the following code:

response = open(url)

This doesn't seem to return anything of use in the way of an access token (it has a @base_uri which is the url I submitted above and few other parameters, though nothing useful looking). However, if I take that url I submitted and paste it into a browser, I receive back an access token.

Can anyone tell me how I can get the request back from facebook and pull out the access token?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瀞厅☆埖开 2024-09-11 17:30:58

假设您正在使用 open-uri,您应该会获得带有一些扩展名的文件句柄。

这意味着您可以像读取任何文件句柄一样读取响应的正文。

body = response.read

然后你可能想要解析 json 并提取访问令牌:

require 'json'
JSON.parse(body)['access_token']

assuming you are using open-uri, you should be getting a file handle back with some extensions.

That means you can get the body of the response by reading it like any file handle.

body = response.read

Then you probably want to parse the json and pull out the access token:

require 'json'
JSON.parse(body)['access_token']
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文