github 的 oauth 登陆之 curl
发布于 作者:苏南大叔 来源:程序如此灵动~

欢迎来到,由苏南大叔给大家带来的“github的oauth登陆系列”文章。该系列文章的代码已经发布到了github上,代码地址是:https://github.com/newsn/typecho-plugin-github-oauth 。大家觉得不错的话,请给我star。本篇文章里,苏南大叔将要讲述,在服务器和github服务器用curl通信的时候的两个注意事项。这个应该是和github服务器通信的时候,所特有的设定。理论上来说,也许其他的oauth通信用不到这些设置。
一,主动发送agent信息
报错信息:
Request forbidden by administrative rules. Please make sure your request has a User-Agent header (http://developer.github.com/v3/#user-agent-required). Check https://developer.github.com for other possible causes.
解决方案:
curl_setopt($ci, CURLOPT_USERAGENT, "newsn.net oauth client");
就是说,你的curl请求里面,必须有agent信息。其实,这个对于我们的服务器端数据采集行为还是有启示的。你懂的。agent你应该设置一个常见的字符串。而这里,我是随便写了点信息。对,随便写的信息。github服务器就是这么矫情,随便写的数据都ok,就是不能不写.... 唉....
二,主动设置返回值格式
$headers[] = "Accept: application/json";
//$headers[]="Accept: application/xml";
curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
总结
由于,接口返回的可以是xml
还可以是json
。而github
的服务器接口就恰好支持两种情况。所以,我们通过header
主动指定希望的返回值格式。这样的话,我们的后续程序处理才会更加明确哦。
这个github的oauth登陆就基本设置完毕。大家记得给我star。谢谢。


