编程语言
首页 > 编程语言> > php-DeviantArt中的用户身份验证

php-DeviantArt中的用户身份验证

作者:互联网

我正在尝试开发一个可以帮助我及时地在各种平台上张贴艺术品的应用程序.

所以我想做的就是使用DeviantArt从PHPAPI中获取身份验证代码授权从DeviantArt获取身份验证代码,然后将其放入我的应用程序中,以便可以使用它来获取访问令牌.并在我的帐户上张贴艺术品.

我已经在下面的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>

UPDATE 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;
    }
?>

解决方法:

您可能需要阅读this link和不成功的示例.

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

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"}

标签:http,oauth-2-0,php,deviantart-api
来源: https://codeday.me/bug/20191108/2005490.html