imsas / wpdm-rest-api-docs

WordPress Download Manager REST API Documentation
Other
1 stars 0 forks source link

Can not authenticate #17

Open DASAT opened 2 years ago

DASAT commented 2 years ago

Hi, I installed the REST API and I tried to get a list of all posts - I can connect to the WordPress Installation but I can not authenticate. What do I do wrong?

I'm using the curl class https://github.com/dcai/curl - and it works fine with all other APIs I use.

Thank you all.

<?php
$authorization = 'Authorization: Bearer xxxxxxxxxxxx'; // API Key generated in the REST API Addon - Doesnt work // even if I try // $authorization = 'Authorization: Basic {base64("xxx:yyyyyyyy1")'; //Username and Password - Doesn't work

$url ="https://mydomain/wp-json/wpdm/v1/packages/";

    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $authorization );
    curl_setopt($curl, CURLOPT_HTTPHEADER, "Content-Type: application/json" );
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   $resp = curl_exec($curl);
    curl_close($curl);
    var_dump($resp);

    var_dump($j_result);

//string(94) "{"code":"rest_forbidden","message":"You cannot view the post resource.","data":{"status":401}}" ?>

codename065 commented 2 years ago

Add the following rule in your .htaccess file: RewriteEngine On RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

jsaalfeld commented 1 year ago

We actually added the following line into the Apache directives in PLESK Host-Setting for HTTP and HTTPS serving (from https://talk.plesk.com/threads/empty-authorization-header-on-php-with-nginx.358618/):

SetEnvIf Authorization "(.*)" HTTP_AUTHORIZATION=$1

This solved the issue of getting a

{
  "code": "rest_forbidden",
  "message": "You cannot view the post.",
  "data": {
    "status": 401
  }
}

If you queried anything besides the root of the REST-API.