[英]Getting a user access token in facebook using Koala
我正在写一个简单的程序,自动发布Facebook帖子。 据我所知,我需要一个“用户访问令牌”才能做到这一点。 我正在使用考拉(但其他图书馆的理念相似)。 无论如何,我创建了一个新的OAuth帐户:
@oauth = Koala::Facebook::OAuth.new(app_id, app_secret, callback_url)
考拉指令然后变得有些不清楚。 接下来的两行是:
@oauth.url_for_oauth_code # generate authenticating URL
@oauth.get_access_token(code) # fetch the access token once you have the code
“代码”变量来自哪里? 它没有在文档中说明。 此外,“get_access_token”方法是否获得“app access token”或“user_access_token”? 方法名称不清楚。 我试着去找[url_for_oauth_code]方法给我的网址,但它没有给我代码! “代码”变量来自哪里?
在Koala的首页上,它声明您需要完成http://developers.facebook.com/docs/authentication/中描述的OAuth流程(这是一个旧链接,但其中的内容有效)
特别
@oauth.url_for_oauth_code
https://github.com/arsduo/koala/blob/master/lib/koala/oauth.rb#L85生成一个URL,您需要将用户定向到基于repo的URL。
https://www.facebook.com/dialog/oauth?
client_id={app-id}&
redirect_uri={redirect-uri}&
scope=email
根据文档https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow/v2.2#login ,当省略response_type
时,默认响应类型为code
。 所以上面相当于
https://www.facebook.com/dialog/oauth?
client_id={app-id}&
response_type=code&
redirect_uri={redirect-uri}&
scope=email
因此,在重定向到redirect-uri
,此URL将附加您必须处理然后提供的代码参数
@oauth.get_access_token(code)
访问令牌是用户访问令牌。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.