Jan Jan - 12 days ago 6
PHP Question

Issue with deviantart authentication (PHP)

I am trying to connect to deviantart via their oauth2 mechanism. I do get the code to obtain an access token but then I get some strange 404 error when trying to get the access token via file_get_contents. However, when I copy the url I am able to get the access token as desired. Has this sth. do with header settings? This is the code to get the access token:

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

Jan Jan
Answer

It took me hours but apparently, deviantart wants to have a USER_AGENT set. So for the records, this works flawlessly (mind the user_agent variable)

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

EDIT: For the records, we have programmed a wrapper for some deviantart functions and have uploaded a small library on github.