-
-
Notifications
You must be signed in to change notification settings - Fork 71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Help needed. Dynamic API token #21
Comments
No need to do all this in the constructor. Just, in your controller method: $c = DigitalOcean::getFactory()->make(['driver' => 'guzzlehttp', 'token' => Auth::user()->do_api_token]); Also, you definitely shouldn't ever need to write "new factory". That should always set off alarm bells. You should always check the source code to see if it's registered in the service container. In the case of this package it is, but you can also access it from the facade as in my first example. If you wanted a dependency injection approach, you could have: use GrahamCampbell\DigitalOcean\DigitalOceanFactory;
// ...
public function myHandler(DigitalOceanFactory $do)
{
$c = $do->make(['driver' => 'guzzlehttp', 'token' => Auth::user()->do_api_token]);
} |
When I do this, I am getting invalid Argument Exception 'The DigitalOcean factory requires an auth method' |
@ppshobi That's because that code example was for an old version of this package. In the new version, you don't need to specify a driver (in fact, if you do, it just gets ignored), but you must now specify an authentication method. Currently, there's a choice of $client = DigitalOcean::getFactory()->make(['method' => 'token', 'token' => 'YOUR-TOKEN-HERE']); |
You can also look at the tests to see how the factory should be used: https://github.com/GrahamCampbell/Laravel-DigitalOcean/blob/7.0/tests/DigitalOceanFactoryTest.php#L31-L39. |
Yes. I was able to find it. Thanks for the library and quick help.
…On Thu, 30 Jul 2020, 1:28 am Graham Campbell, ***@***.***> wrote:
You can also look at the tests to see how the factory should be used:
https://github.com/GrahamCampbell/Laravel-DigitalOcean/blob/7.0/tests/DigitalOceanFactoryTest.php#L31-L39
.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#21 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ACBEEHYXRNRAKZHK3VNWWELR6CPCJANCNFSM4FHJXBRA>
.
|
So far I have this put in a
InitializeDO
method. Not sure if it's the right way but it works just fine. Correct me if there's a better way to set the API token dynamically.The problem is that I can't access the user(
Auth::user()
) in the consturctor of the controller, so I'm callingInitializeDO
in each one of my methods(index, show, edit, etc.). I don't think this is the best approach.And here comes my question, how can I write this, so it's called just once and working with $this->do afterwards?
The text was updated successfully, but these errors were encountered: