facebook错误'验证验证码时出错'

kus*_*agi 54 php facebook urlencode

非常奇怪的错误.我使用gide http://developers.facebook.com/docs/authentication/.所以我创建了对fb的请求并传递了redirect_uri.我在localhost上使用测试站点.所以,如果我通过

redirect_uri = http://localhost/test_blog/index.php

它工作正常,但如果我通过

redirect_uri = http://localhost/test_blog/index.php?r = site/oauth2

它不想要工作.我尝试使用

redirect_uri =.urlencode(' http://localhost/test_blog/index.php?r = site/oauth2)

但不行.我尝试解释.我成功获得代码,但当我访问https://graph.facebook.com/me?access_token时,我收到错误'错误验证验证码'.我检查过,错误是在?r = site/oauth2但我需要传递一些参数可以有人帮助我吗?我读了http://forum.developers.facebook.net/viewtopic.php?id=70855的帖子,但对我来说没什么用

小智 128

目前(截至2011年3月)有关什么使有效redirect_uri的无证要求.

首先,要授权的redirect_uri参数和access_token必须匹配.

显然,Facebook(或更确切地说是OAuth2)使用redirect_uri作为内部密钥来编码为access_token请求返回的代码.它有点聪明,因为它验证回到您的网站.它解释了为什么access_token请求本来不需要redirect_uri参数需要一个.

其次,你不能在redirect_uri中使用很多特殊字符.

许多讨论都在争论是否可以传递参数.他们可以,你有限的哪些字符是有效的,但没有人发布我知道的列表.url/html编码等传统方法将失败,因为百分比(%)无效.斜杠(/)无效,因此嵌套的重定向URL将始终失败.克服特殊字符限制的唯一方法是将参数的值编码为base64.如果您使用的是ASP.NET,请查找Convert.ToBase64.

最后,这更像是一个侧面说明.有很多程序员传递错误信息,一个简单的解决方案是传递type = client_cred.这可能会限制您访问授权中请求的某些权限.这是不可取的.

  • 不知道redirect_uri必须在两者上匹配(虽然这是有意义的).这个问题继续给予,甚至几个月后:) (3认同)

小智 52

redirect_uri=http://localhost:8000(编码http%3A%2F%2Flocalhost%3A8000)测试时整天都有同样的问题......

解决方法只是确保将尾部斜杠/放在uri的末尾.所以redirect_uri=http://localhost:8000/(编码http%3A%2F%2Flocalhost%3A8000%2F).

同样,确保redirect_uri两个请求都相同.

  • 2小时......浪费2个小时的时间...谢谢 (9认同)

Ada*_*ite 17

我有这个问题.我知道我的URL是相同的,因为我使用了一个具有相同$ var的类,但我在JSON响应中不断得到400响应和错误.

我唯一做的就是改变我的redirect_uri:

http://myredirecturi.com
Run Code Online (Sandbox Code Playgroud)

http://myredirecturi.com/
Run Code Online (Sandbox Code Playgroud)

是的,只是添加了尾随斜杠并且它有效.


Niz*_* B. 8

你真的不需要编码,只需将'/'放在redirect_url的末尾,一切都应该没问题!

  • 为我工作!谢谢! (2认同)

Joh*_*ohn 6

Aaron Wheeler提供的部分信息不正确.

确实,'redirect_uri'参数在两个请求中必须完全相同,但是完全可以对常规URL进行URL编码并将其用作'redirect_url'参数的值,只要您小心进一步的URL即可编码任何内联网址.

例如,您希望Facebook重定向到以下URL:

http://www.mysite.com/Users/oAuthComplete?my_param_1=/Party/pants
Run Code Online (Sandbox Code Playgroud)

试图将用户重定向到

'https://www.facebook.com/dialog/oauth?client_id=12345&redirect_uri='
. urlencode('http://www.mysite.com/Users/oAuthComplete?my_param_1=/Party/pants');
Run Code Online (Sandbox Code Playgroud)

将失败,因为/Party/Pants创建了无效的URL

但是,重定向到

'https://www.facebook.com/dialog/oauth?client_id=12345&redirect_uri='
.urlencode('http://www.mysite.com/Users/oAuthComplete?my_param_1='
.urlencode('/Party/pants'));
Run Code Online (Sandbox Code Playgroud)

将按预期工作.

如果您在第二个验证应用程序请求中使用返回的redrect_uri值,请确保再次进行url编码 - 在填充$ _GET超全局时,该值会自动进行URL解码. - 这就是让我失望的原因.

'https://graph.facebook.com/oauth/access_token?client_id=12345&&client_secret=SECRET&code=1234567'
.urlencode('http://www.mysite.com/Users/oAuthComplete?my_param_1='
.urlencode($_GET['my_param_1']));
Run Code Online (Sandbox Code Playgroud)

Ps在你的实际代码中,我建议使用sprintf()而不是像我的例子一样将字符串链接在一起,以提高可读性.


小智 5

从我所看到的,这里的问题是redirect_uri必须以'/'结尾而不包含'?' 或其他特殊字符.我认为这就是为什么你得到'错误验证验证码'的原因.只有在使用file_get_contents()时才会出现此错误,而在使用facebook php库时则不会出现此错误.这是php的解决方案,不知道这个错误是否出现在其他SDK中.