2

我正在尝试制作一个应用程序,可以帮助我以定时的方式一次在各种平台上发布艺术作品。

所以我想要做的只是使用他们在 PHP 中的API的身份验证代码授予从 DeviantArt 获取身份验证代码,然后我将获取该代码并将其放在我的应用程序中,以便我可以使用它来获取访问令牌并在我的帐户上发布艺术作品。

我已经制作了下面的 PHP 代码来向https://www.deviantart.com/oauth2/authorize发出 GET 请求,它成功地将我发送到身份验证页面。但是当我登录到我的帐户时,它给了我一个 403 Forbidden Access 错误。API 要求我发布我的应用程序并将 OAtuth2 重定向 URI 列入白名单, 我已经这样做了。

这是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错误。我还发现如果我给redirect_uri属性一个未列入白名单的URI,它将显示一个错误页面,而不是要求我登录。我还尝试要求它将我重定向到另一个网页(youtube),但没有成功(仍然给我 403 错误)。

更新 02:其中一个答案表明该错误可能是因为我需要在标头中发送带有“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;
}
?>

上面的代码仍然没有解决问题,可能是因为我需要“压缩”我的 HTTP GET 请求?我很新,我试图做一些研究,但我不知道该怎么做。

更新 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;
    }
?>
4

3 回答 3

1

您可能想阅读此链接以及其中的不成功/成功示例。

在我看来,这些变量是必需的:

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"}
于 2019-05-05T17:39:04.190 回答
1

我不知道您是否这样做,但如果您不发送用户代理并且不在请求中使用压缩,您可能会收到 403。IIRC 在过去的某个时候,他们在没有事先通知的情况下默默地开始执行这两项。一切都突然失败很有趣。

引用DA 错误部分

如果您收到包含 HTML 响应而不是 JSON 的 403 错误,请确保您的客户端正在发送用户代理标头并对请求使用 HTTP 压缩,我们会拒绝任何不符合此要求的请求。

于 2019-05-05T19:31:58.570 回答
0

我发现了问题所在。显然,我不应该获取身份验证页面的内容并在我的页面中回显。相反,我应该将用户重定向到身份验证页面。

<?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");
    }
?> 
于 2019-05-07T22:48:11.463 回答