使用Facebook OAuth 2.0-如何获取访问令牌? [英] Using Facebook OAuth 2.0 - How do I fetch the access token?

查看:80
本文介绍了使用Facebook OAuth 2.0-如何获取访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 OAuth 的新手,我正在尝试使用 Facebook Connect 与我的Web应用程序配合.

I am new to OAuth, and I'm trying to use Facebook Connect with my web-application.

我成功获取了验证令牌,但是我的问题是获取"访问令牌.我该如何获取? Facebook文档告诉我要使用以下URL来获取访问令牌:

I have succeded in getting a verification token, but my problem is "fetching" the access token. How do I fetch it? The Facebook documentation tells me to fetch the access token with this URL:

https://graph.facebook.com/oauth/access_token?'
                + 'client_id=XXXXXXXXXXXX& redirect_uri=http://www.mysite.com/fbconn/index.html&display=touch&'
                + 'client_secret=axxxxxcxxxxxxxxxxx&code=' + code;

当我使用它时,我会在空白页上看到访问令牌,但是我想使用JavaScript(Ajax),PHP或其他东西来获取它.这可能吗?我以为访问令牌会像验证代码一样附加到我的重定向URI上,但是我从来没有重定向到我的页面.我在做什么错了?

When I use this I see the access token on a blank page, but I want to fetch it with JavaScript (Ajax), PHP or something. Is this possible? I thought the access token would be appended to my redirect URI like the verfication code, but I never get redirected to my page. What am I doing wrong?

推荐答案

您需要将&type=user_agent添加到请求中.您将以以下格式获得带有哈希标记的AuthToken.

You need to add &type=user_agent to the request. You will get the AuthToken with a hash marker in the following format.

http://yourredirecturi#code=[accesstoken]

如果将请求设置为&type=web_server,则将获得AuthToken作为查询字符串参数:http://yourredirecturi&code=[accesstoken].

If you set the request to &type=web_server, you'll get the AuthToken as a query string parameter: http://yourredirecturi&code=[accesstoken].

这里是有关如何实现Facebook的OAuth协议的完整说明.这些代码示例在ASP.NET MVC中,但是它应该可以很好地转换为任何语言:

Here is a full explanation of how to implement the Facebook's OAuth protocol. The code samples are in ASP.NET MVC, but it should translate well enough to any language:

Facebook平台的OAuth 2.0协议和ASP.NET MVC

这篇关于使用Facebook OAuth 2.0-如何获取访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆