简体   繁体   English

如何从Facebook回调URL解析`access_token`?

[英]How can I parse the `access_token` from a Facebook callback URL?

This is the request Facebook calls back: 这是Facebook回叫的请求:

/facebook/promo/#access_token=162592277090170%7C2.yCch3otjrdit_rgBFS6F0A__.3600.1285369200-727781553%7CtugQmoO0bRiadumHVQrrSiPRD9Y&expires_in=7174

How can I parse the access_token from the URL? 如何从URL解析access_token I could not find any way to get the access_token value. 我找不到任何方法来获取access_token值。

Please be aware that it is not a reqular parameter. 请注意,它不是必需参数。

If you're only after the access_token=... section, just use some simple string matching: 如果仅在access_token=...部分之后,请使用一些简单的字符串匹配:

url = '/facebook/promo/#access_token=162592277090170%7C2.yCch3otjrdit_rgBFS6F0A__.3600.1285369200-727781553%7CtugQmoO0bRiadumHVQrrSiPRD9Y&expires_in=7174'
url[/#access_token=(.+)&/, 1]
=> "162592277090170%7C2.yCch3otjrdit_rgBFS6F0A__.3600.1285369200-727781553%7CtugQmoO0bRiadumHVQrrSiPRD9Y"

That looks for #access_token= and grabs everything up to & . 该命令查找#access_token=并抓取直到&所有内容。

You could use a Regex to match it out of the url. 您可以使用正则表达式将其与网址匹配。 Or simply take everything as a sub-string between access_token= and the next & -character or the end of the url, which ever comes first. 或者只是将所有内容都作为access_token=与下一个& -字符或url结束之间的子字符串(以先到者为准)。

I believe that it's not possible - the part after # is simply ignored. 我相信这是不可能的-#之后的部分将被忽略。 See this answer: Rails: Extracting the raw url from the request 看到这个答案: Rails:从请求中提取原始URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM