DigitalOceanPHP / Client

DigitalOcean API v2 client for PHP
MIT License
709 stars 205 forks source link

Unable to authenticate you. #238

Closed turigeza closed 4 years ago

turigeza commented 5 years ago

Following you example in the readme I am getting this error. I have tried to regenerate the API key but still the same. Any ideas ? Thank you.

Fatal error: Uncaught DigitalOceanV2\Exception\HttpException: Unable to authenticate you. in /websites/aapi.younit.app/vendor/toin0u/digitalocean-v2/src/Adapter/BuzzAdapter.php:145 Stack trace: #0 /websites/aapi.younit.app/vendor/toin0u/digitalocean-v2/src/Adapter/BuzzAdapter.php(130): DigitalOceanV2\Adapter\BuzzAdapter->handleError(Object(Buzz\Message\Response)) #1 /websites/aapi.younit.app/vendor/toin0u/digitalocean-v2/src/Adapter/BuzzAdapter.php(50): DigitalOceanV2\Adapter\BuzzAdapter->handleResponse(Object(Buzz\Message\Response)) #2 /websites/aapi.younit.app/vendor/toin0u/digitalocean-v2/src/Api/Domain.php(31): DigitalOceanV2\Adapter\BuzzAdapter->get('https://api.dig...') #3 /websites/aapi.younit.app/cluster/check_ssh_key.php(28): DigitalOceanV2\Api\Domain->getAll() #4 /websites/aapi.younit.app/cluster/check_ssh_key.php(31): {closure}() #5 {main} thrown in /websites/aapi.younit.app/vendor/toin0u/digitalocean-v2/src/Adapter/BuzzAdapter.php on line 145

PHP Version 7.2.15-0ubuntu0.18.04.1 cURL support enabled cURL Information 7.58.0

So it seems that GuzzleHttpAdapter works fine. Only BuzzAdapter giving me the grief. Instead of this

require 'vendor/autoload.php';

use DigitalOceanV2\Adapter\BuzzAdapter;
use DigitalOceanV2\DigitalOceanV2;

// create an adapter with your access token which can be
// generated at https://cloud.digitalocean.com/settings/applications
$adapter = new BuzzAdapter('your_access_token');

// create a digital ocean object with the previous adapter
$digitalocean = new DigitalOceanV2($adapter);

I use this

require 'vendor/autoload.php';
use DigitalOceanV2\Adapter\GuzzleHttpAdapter;
use DigitalOceanV2\DigitalOceanV2;
$adapter = new GuzzleHttpAdapter('your_access_token');
$digitalocean = new DigitalOceanV2($adapter);
glushchenko commented 4 years ago

Solved. Your should instantiate Buzz with middleware, otherwise Header token not be added.

$curl = new \Buzz\Client\Curl();
$browser = new \Buzz\Browser($curl);
$browser->addMiddleware(
    new \Buzz\Middleware\BearerAuthMiddleware('token')
);

$adapter = new BuzzAdapter(null, $browser);
$digitalocean = new DigitalOceanV2($adapter);
bitwombat commented 4 years ago

Does this mean the examples need to be updated?

consigliere23 commented 4 years ago

Does this mean the examples need to be updated?

seems not :/

glushchenko commented 4 years ago

@consigliere23 seems? Examples not working anymore. It's exactly.

glennjacobs commented 4 years ago

If the examples are out of date, can you help by creating a pull request for the changes required?

GrahamCampbell commented 4 years ago

~Moving to part of the v4 design issue #249. I am planning to smash out a fresh package for v4 which will feel like the old package, but have none of the issues and all of the compatibility. ;)~

EDIT: Robust implementations of Buzz 0.16 and Guzzle 6 adapters are implemented in the 3.0.0 release.

GrahamCampbell commented 4 years ago

New code for v3 of the library:

<?php

require_once 'vendor/autoload.php';

$digitalocean = new DigitalOceanV2\Client();

$digitalocean->authenticate('your_access_token');

No need to do anything with adapters. As long as the current version of Guzzle or Buzz is installed, we will automatically discover it.