DeviantArt 中的用户身份验证
User authentication in DeviantArt
我正在尝试制作一个可以帮助我 post 定时在各种平台上进行艺术创作的应用程序。
所以我要做的就是使用他们在 PHP 中的 API 的身份验证代码授权从 DeviantArt 获取身份验证代码,然后我将获取该代码并将其放入在我的应用程序上,这样我就可以用它来获取访问令牌和 post 我帐户上的艺术品。
我已经使用下面的 PHP 代码向 https://www.deviantart.com/oauth2/authorize 发出 GET 请求,它成功地将我发送到身份验证页面。但是当我登录到我的帐户时,它给我一个 403 禁止访问错误。 API 要求我发布我的应用程序并将 OAtuth2 重定向 URI 列入白名单,并且
I've already done that.
这是 poster/assets/php/authDeviantArt 中的代码。inc.php:
<?php
if(isset($_POST["submit"])){
$result = file_get_contents("https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress:80/poster/requestAuthorization.php", false);
echo $result;
} ?>
这是 DeviantArt 应该重定向我的 URI 中的文件(poster/requestAuthorization。php):
<?php
if(isset($_GET["code"])){
echo $_GET["code"];
}
?>
<html>
<head>
<title>Authorization</title>
<meta charset="utf-8">
</head>
<body>
<form action="assets/php/authDeviantArt.inc.php" method="POST">
<button type="submit" name="submit">Authorization DeviantArt</button>
</form>
</body>
</html>
更新 01: 我已经从白名单和 redirect_uri 中的 URI 中删除了端口,但在我登录时它仍然给我 403 错误in。我还发现,如果我给 redirect_uri 属性 一个未列入白名单的 URI,它会显示一个错误页面,而不是要求我登录。我也试过要求它重定向我转到另一个网页 (youtube),但没有成功(仍然给我 403 错误)。
UPDATE 02: 其中一个答案表明错误可能是因为我需要在 header 中发送带有 "USER-AGENT" 的请求并压缩它。现在我的代码是这样的:
<?php
if(isset($_POST["submit"])){
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"User-agent: ".$_SERVER["HTTP_USER_AGENT"]
)
);
$context = stream_context_create($opts);
$result = file_get_contents("https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress/poster/requestAuthorization.php", false, $context);
echo $result;
}
?>
上面的代码还是没有解决问题,可能是因为我需要"compress"我的HTTP GET请求?我是新手,我尝试做一些研究,但我找不到如何去做。
UPDATE 03: 感谢下面的答案之一,我找到了如何压缩我的请求。不幸的是,它没有用。我会将电子邮件发送给 DeviantArt 的支持团队,看看他们是否可以帮助我。我的代码现在是这样的:
<?php
if(isset($_POST["submit"])){
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"User-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n".
"Accept-Encoding: gzip\r\n"
)
);
$context = stream_context_create($opts);
$result = file_get_contents("compress.zlib://https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress/poster/requestAuthorization.php", false, $context);
echo $result;
}
?>
您可能想阅读 this link 和 un/successful 个示例。
在我看来,这些变量是必需的:
curl https://www.deviantart.com/oauth2/token \
-d grant_type=authorization_code \
-d client_id=0 \
-d client_secret=mysecret \
-d redirect_uri=http://myapp.example/cb \
-d code=1234
成功案例
{
"expires_in": 3600,
"status": "success",
"access_token": "Alph4num3r1ct0k3nv4lu3",
"token_type": "Bearer"
"refresh_token": "3ul4vn3k0tc1r3mun4hplA",
"scope": "basic"
}
根据示例,也许您只是缺少 client_secret
变量,如果您愿意,可以添加一个 grant_type
.
<?php
if(isset($_POST["submit"])){
$result = file_get_contents("https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress:80/poster/requestAuthorization.php", false);
echo $result;
} ?>
然而,他们的错误信息似乎并不需要这些变量:
{"error":"invalid_request","error_description":"Request field validation failed.","error_details":{"response_type":"response_type is required","client_id":"client_id is required","redirect_uri":"redirect_uri is required"},"status":"error"}
我不知道您是否这样做,但如果您不发送用户代理并且不在您的请求中使用压缩,您可能会收到 403。
IIRC 在过去的某个时候,他们在没有事先通知的情况下默默地开始执行这两项。一切都突然失败很有趣。
If you recieve 403 errors that contain an HTML response rather than JSON, please ensure your client is sending a User Agent header and using HTTP compression for the request, we reject any requests not meeting this requirement.
我发现了问题所在。
显然,我不应该获取身份验证页面的内容并在我的页面中回显。相反,我应该将用户重定向到身份验证页面。
<?php
if(isset($_POST["submit"])){
header("Location: https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress/poster/requestAuthorization.php");
}
?>
我正在尝试制作一个可以帮助我 post 定时在各种平台上进行艺术创作的应用程序。
所以我要做的就是使用他们在 PHP 中的 API 的身份验证代码授权从 DeviantArt 获取身份验证代码,然后我将获取该代码并将其放入在我的应用程序上,这样我就可以用它来获取访问令牌和 post 我帐户上的艺术品。
我已经使用下面的 PHP 代码向 https://www.deviantart.com/oauth2/authorize 发出 GET 请求,它成功地将我发送到身份验证页面。但是当我登录到我的帐户时,它给我一个 403 禁止访问错误。 API 要求我发布我的应用程序并将 OAtuth2 重定向 URI 列入白名单,并且 I've already done that.
这是 poster/assets/php/authDeviantArt 中的代码。inc.php:
<?php
if(isset($_POST["submit"])){
$result = file_get_contents("https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress:80/poster/requestAuthorization.php", false);
echo $result;
} ?>
这是 DeviantArt 应该重定向我的 URI 中的文件(poster/requestAuthorization。php):
<?php
if(isset($_GET["code"])){
echo $_GET["code"];
}
?>
<html>
<head>
<title>Authorization</title>
<meta charset="utf-8">
</head>
<body>
<form action="assets/php/authDeviantArt.inc.php" method="POST">
<button type="submit" name="submit">Authorization DeviantArt</button>
</form>
</body>
</html>
更新 01: 我已经从白名单和 redirect_uri 中的 URI 中删除了端口,但在我登录时它仍然给我 403 错误in。我还发现,如果我给 redirect_uri 属性 一个未列入白名单的 URI,它会显示一个错误页面,而不是要求我登录。我也试过要求它重定向我转到另一个网页 (youtube),但没有成功(仍然给我 403 错误)。
UPDATE 02: 其中一个答案表明错误可能是因为我需要在 header 中发送带有 "USER-AGENT" 的请求并压缩它。现在我的代码是这样的:
<?php
if(isset($_POST["submit"])){
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"User-agent: ".$_SERVER["HTTP_USER_AGENT"]
)
);
$context = stream_context_create($opts);
$result = file_get_contents("https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress/poster/requestAuthorization.php", false, $context);
echo $result;
}
?>
上面的代码还是没有解决问题,可能是因为我需要"compress"我的HTTP GET请求?我是新手,我尝试做一些研究,但我找不到如何去做。
UPDATE 03: 感谢下面的答案之一,我找到了如何压缩我的请求。不幸的是,它没有用。我会将电子邮件发送给 DeviantArt 的支持团队,看看他们是否可以帮助我。我的代码现在是这样的:
<?php
if(isset($_POST["submit"])){
$opts = array(
'http' => array(
'method'=>"GET",
'header'=>"User-Agent: ".$_SERVER["HTTP_USER_AGENT"]."\r\n".
"Accept-Encoding: gzip\r\n"
)
);
$context = stream_context_create($opts);
$result = file_get_contents("compress.zlib://https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress/poster/requestAuthorization.php", false, $context);
echo $result;
}
?>
您可能想阅读 this link 和 un/successful 个示例。
在我看来,这些变量是必需的:
curl https://www.deviantart.com/oauth2/token \
-d grant_type=authorization_code \
-d client_id=0 \
-d client_secret=mysecret \
-d redirect_uri=http://myapp.example/cb \
-d code=1234
成功案例
{
"expires_in": 3600,
"status": "success",
"access_token": "Alph4num3r1ct0k3nv4lu3",
"token_type": "Bearer"
"refresh_token": "3ul4vn3k0tc1r3mun4hplA",
"scope": "basic"
}
根据示例,也许您只是缺少 client_secret
变量,如果您愿意,可以添加一个 grant_type
.
<?php
if(isset($_POST["submit"])){
$result = file_get_contents("https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress:80/poster/requestAuthorization.php", false);
echo $result;
} ?>
然而,他们的错误信息似乎并不需要这些变量:
{"error":"invalid_request","error_description":"Request field validation failed.","error_details":{"response_type":"response_type is required","client_id":"client_id is required","redirect_uri":"redirect_uri is required"},"status":"error"}
我不知道您是否这样做,但如果您不发送用户代理并且不在您的请求中使用压缩,您可能会收到 403。 IIRC 在过去的某个时候,他们在没有事先通知的情况下默默地开始执行这两项。一切都突然失败很有趣。
If you recieve 403 errors that contain an HTML response rather than JSON, please ensure your client is sending a User Agent header and using HTTP compression for the request, we reject any requests not meeting this requirement.
我发现了问题所在。 显然,我不应该获取身份验证页面的内容并在我的页面中回显。相反,我应该将用户重定向到身份验证页面。
<?php
if(isset($_POST["submit"])){
header("Location: https://www.deviantart.com/oauth2/authorize?response_type=code&client_id=9685&redirect_uri=http://myipaddress/poster/requestAuthorization.php");
}
?>