Twitter仅应用程序身份验证PHP OAuth错误
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Twitter仅应用程序身份验证PHP OAuth错误,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4031字,纯文字阅读大概需要6分钟。
内容图文
![Twitter仅应用程序身份验证PHP OAuth错误](/upload/InfoBanner/zyjiaocheng/677/d4bab32889c44ddfa5400d5c624ad17f.jpg)
请注意
现在,可以在以下github链接中找到有效的Twitter应用程序仅身份验证Oauth PHP库的完整实现:??Twitter Application Only Authentication OAuth Php
我正在尝试让我的应用程序通过Twitter的仅应用程序身份验证进行身份验证.请参阅以下URL的文档:
Twitter Developer Documentation fro Application-only authentication
我进入第2步,并使用下面粘贴的代码请求承载令牌.我收到如下响应:
HTTP/1.1 200 OK
Date: Tue, 19 Mar 2013 15:13:03 GMT
Status: 200 OK
X-Frame-Options: DENY
ETag: "6b7ec13d0ef1e9d8e0b39bec5266ba7b"
X-Runtime: 0.06008
Pragma: no-cache
Last-Modified: Tue, 19 Mar 2013 15:13:03 GMT
Content-Type: application/json; charset=utf-8
X-Transaction: 55809f2187d74c8e
X-MID: 4503f89925628df071cb1d27c3e6953709234b1b
Expires: Tue, 31 Mar 1981 05:00:00 GMT
Cache-Control: no-cache, no-store, must-revalidate, pre-check=0, post-check=0
Set-Cookie: k=10.37.190.122.1363705983314067; path=/; expires=Tue, 26-Mar-13 15:13:03 GMT; domain=.twitter.com
Set-Cookie: _twitter_sess=BAh7CSIKZmxhc2hJQzonQWN0aW9uQ29udHJvbGxlcjo6Rmxhc2g6OkZsYXNo%250ASGFzaHsABjoKQHVzZWR7ADoMY3NyZl9pZCIlZTI5MjJmNDI5ODY4MzM0NjAz%250AMzkwZTE2NDY5MzdiNGM6D2NyZWF0ZWRfYXRsKwhV8TWDPQE6B2lkIiU2NzEy%250AZjgzOTNjYjc4NjNhYzgwMjU2Mjc1Yzc0ZDYyMw%253D%253D--22b3f64a757c1f9f3c3ae8df9ee434848c43eee8; domain=.twitter.com; path=/; HttpOnly
Vary: Accept-Encoding
Content-Encoding: gzip
Content-Length: 138
Server: tfe
Set-Cookie: guest_id=v1%3A136370598331525689; Domain=.twitter.com; Path=/; Expires=Thu, 19-Mar-2015 15:13:03 UTC
??V*é?Níx?/?,HU2RJJM,J-RòQJLNN-.?Kr?±?|sg(?¢ê(?5*Y???G?è-ì?x??2(D?ù(í4?í?D??¥2+è3???¨ê7?à?$,+??"?35
如您所见,返回的内容似乎没有返回nto,如文档中所述
HTTP/1.1 200 OK
Status: 200 OK
Content-Type: application/json; charset=utf-8
...
Content-Encoding: gzip
Content-Length: 140
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}
如我们所见
??V*é?Níx?/?,HU2RJJM,J-RòQJLNN-.?Kr?±?|sg(?¢ê(?5*Y???G?è-ì?x??2(D?ù(í4?í?D??¥2+è3???¨ê7?à?$,+??"?35
不等于
{"token_type":"bearer","access_token":"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2FAAAAAAAAAAAAAAAAAAAA%3DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"}
我想知道这是否可能是编码错误,或者我是否从根本上对cURL做错了.
我的代码粘贴在下面,任何帮助将不胜感激:
乔恩
<?php
// from https://dev.twitter.com/docs/auth/application-only-auth
$consumer_key = 'myconsumerkey';
$consumer_secret = 'myconsumersecret';
// step 1
// step 1.1 - url encode the consumer_key and consumer_secret in accordance with RFC 1738
$encoded_consumer_key = urlencode($consumer_key);
$encoded_consumer_secret = urlencode($consumer_secret);
// step 1.2 - concatinate encoded consumer, a colon character and the encoded consumer secr et
$bearer_token = $encoded_consumer_key.':'.$encoded_consumer_secret;
// step 1.3 - base64-encode bearer token
$base64_encoded_bearer_token = base64_encode($bearer_token);
// step 2
$url = "https://api.twitter.com/oauth2/token"; // url to send data to for authentication
$headers = array(
"POST /oauth2/token HTTP/1.1",
"Host: api.twitter.com",
"User-Agent: my Twitter App v.1",
//"Authorization: Basic ".$base64_encoded_bearer_token."",
"Authorization: Basic ".$base64_encoded_bearer_token."",
"Content-Type: application/x-www-form-urlencoded;charset=UTF-8",
"Content-Length: 29",
"Accept-Encoding: gzip"
);
$ch = curl_init(); // setup a curl
curl_setopt($ch, CURLOPT_URL,$url); // set url to send to
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); // set custom headers
curl_setopt($ch, CURLOPT_POST, 1); // send as post
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials"); // post body/fields to be sent
$header = curl_setopt($ch, CURLOPT_HEADER, 1); // send custom headers
//$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$result = curl_exec($ch); // run the curl
curl_close($ch); // stop curling
?>
解决方法:
在评论中由CBroe回答:
我认为Accept-Encoding:gzip标头可能是您在这里遇到的问题–您说您接受了gzip压缩的响应,但是随后您不知道该如何处理…所以不要设置该标头. – CBroe
内容总结
以上是互联网集市为您收集整理的Twitter仅应用程序身份验证PHP OAuth错误全部内容,希望文章能够帮你解决Twitter仅应用程序身份验证PHP OAuth错误所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。