deviantart 身份验证问题 (PHP)
Issue with deviantart authentication (PHP)
我正在尝试通过他们的 oauth2 mechanism 连接到 deviantart。我确实获得了获取访问令牌的代码,但是当我尝试通过 file_get_contents 获取访问令牌时,我遇到了一些奇怪的 404 错误。但是,当我复制 url 时,我能够根据需要获取访问令牌。有这个……使用 header 设置?这是获取访问令牌的代码:
function getAccessToken($code) {
$url = "https://www.deviantart.com/oauth2/token";
$data = array();
$data["grant_type"] = "authorization_code";
$data["client_id"] = $this->client_id;
$data["client_secret"] = $this->client_secret;
$data["redirect_uri"] = $this->redirect_uri;
$data["code"] = $code;
return $this->sendToDeviantArt($url, $data);
}
private function sendToDeviantArt($url=null, $data=array()) {
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
我花了几个小时,但显然,deviantart 想要一套 USER_AGENT。所以为了记录,这个工作完美无缺(注意 user_agent 变量)
private function sendToDeviantArt($url=null, $data=array()) {
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
'user_agent' => $_SERVER["HTTP_USER_AGENT"]
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
编辑: 作为记录,我们为一些 deviantart 函数编写了一个包装器并上传了 a small library on github.
这对我来说也是一种痛苦。感谢 Jan 提醒用户代理。只是想我会提供一个 curl 替代方案,因为这就是我为此使用的。显然编辑你的心满意足,但如果你通过它传递一个 deviantART api url 这有效。
function curl($url) {
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the user agent
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER["HTTP_USER_AGENT"]);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
return $result;
}
```
获取访问令牌的示例用法:
$json = json_decode(curl("https://www.deviantart.com/oauth2/token?response_type=code&client_id=0&client_secret=0&grant_type=client_credentials"), true);
$json["access_token"];
我正在尝试通过他们的 oauth2 mechanism 连接到 deviantart。我确实获得了获取访问令牌的代码,但是当我尝试通过 file_get_contents 获取访问令牌时,我遇到了一些奇怪的 404 错误。但是,当我复制 url 时,我能够根据需要获取访问令牌。有这个……使用 header 设置?这是获取访问令牌的代码:
function getAccessToken($code) {
$url = "https://www.deviantart.com/oauth2/token";
$data = array();
$data["grant_type"] = "authorization_code";
$data["client_id"] = $this->client_id;
$data["client_secret"] = $this->client_secret;
$data["redirect_uri"] = $this->redirect_uri;
$data["code"] = $code;
return $this->sendToDeviantArt($url, $data);
}
private function sendToDeviantArt($url=null, $data=array()) {
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
我花了几个小时,但显然,deviantart 想要一套 USER_AGENT。所以为了记录,这个工作完美无缺(注意 user_agent 变量)
private function sendToDeviantArt($url=null, $data=array()) {
$options = array(
'http' => array(
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'method' => 'POST',
'content' => http_build_query($data),
'user_agent' => $_SERVER["HTTP_USER_AGENT"]
),
);
$context = stream_context_create($options);
$result = file_get_contents($url, false, $context);
return $result;
}
编辑: 作为记录,我们为一些 deviantart 函数编写了一个包装器并上传了 a small library on github.
这对我来说也是一种痛苦。感谢 Jan 提醒用户代理。只是想我会提供一个 curl 替代方案,因为这就是我为此使用的。显然编辑你的心满意足,但如果你通过它传递一个 deviantART api url 这有效。
function curl($url) {
// Initiate curl
$ch = curl_init();
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the user agent
curl_setopt($ch,CURLOPT_USERAGENT,$_SERVER["HTTP_USER_AGENT"]);
// Set the url
curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
// Closing
curl_close($ch);
// Will dump a beauty json :3
return $result;
}
```
获取访问令牌的示例用法:
$json = json_decode(curl("https://www.deviantart.com/oauth2/token?response_type=code&client_id=0&client_secret=0&grant_type=client_credentials"), true);
$json["access_token"];