Skip to content
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

SPARKPOST return 401 Unauthorized #180

Closed
Seeryos opened this issue Jul 5, 2018 · 3 comments
Closed

SPARKPOST return 401 Unauthorized #180

Seeryos opened this issue Jul 5, 2018 · 3 comments

Comments

@Seeryos
Copy link

Seeryos commented Jul 5, 2018

Hi everyone,

I'm trying to implement PHP method from instructions in this github to use sparkpost on my website. So I configure the tool from dashboard and all is valid. Now my function in PHP

`
use SparkPost\SparkPost;
use GuzzleHttp\Client;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$httpClient = new GuzzleAdapter(new Client());
$sparky = new SparkPost($httpClient, ['key'=>'**********']);

$promise = $sparky->transmissions->post([
    'content' => [
        'from' => [
            'name' => 'mydomain',
            'email' => 'support@m.mydomain.fr',
        ],
        'subject' => 'Test',
        'html' => '<html><body><h1>Congratulations, {{name}}!</h1><p>You just sent your very first mailing!</p></body></html>',
        'text' => 'Congratulations, {{name}}!! You just sent your very first mailing!',
    ],
    'substitution_data' => ['name' => 'Yohan'],
    'recipients' => [
        [
            'address' => [
                'name' => 'Yohan',
                'email' => '*******@gmail.com',
            ],
        ],
    ]
]);


$sparky->setOptions(['async' => false]);
try {
    $response = $sparky->transmissions->get();
    jsonKill(Success($response->getStatusCode(). " " . $response->getBody()));
}
catch (\Exception $e) {
	jsonKill(Error($e->getCode(). " " . $e->getMessage()));
}

`

And the server return

{
    "success": false,
    "data": "401 {\"errors\": [ {\"message\": \"Unauthorized.\"} ]}\n"
}

401 Message Unauthorized. I don't understand why. I contact support who tell me "Please ensure that you are using the EU API end point: https://api.eu.sparkpost.com/api/v1"

But there is nowhere where I can put this line in my code...

for your help, THANK YOU VERY Much !

@Seeryos
Copy link
Author

Seeryos commented Jul 5, 2018

Hi, So ! I solved problem with support. So, if someone is in the same case, just open file SparkPost.php in vendor/sparkpost/sparkpost/lib/SparkPost and replace

'host' => 'api.sparkpost.com',
by
'host' => 'api.eu.sparkpost.com',

The problem is due to the location of my Spark account in Europe.

PROBLEM SOLVED

@Seeryos Seeryos closed this as completed Jul 12, 2018
@tochix
Copy link

tochix commented Aug 18, 2018

Or you could inject the host option when creating the SparkPostobject.

$httpClient = new GuzzleAdapter(new Client());
$sparky = new SparkPost($httpClient, ['key'=>'**********', 'host' => 'api.eu.sparkpost.com']);

That way you wouldn't need to change third party library and also remember to re-apply your change when you upgrade to a newer version of the library.

@Flucadetena
Copy link

In case anyone is having the same problem using the node library. I know this is the pho, but is the first in google results.

The solution is to add the param "origin" with the value: "https://api.eu.sparkpost.com:443" like so:

	const client = new SparkPost("YOUR-API-KEY", { origin: "https://api.eu.sparkpost.com:443" });

as stated in the dos: https://github.com/SparkPost/node-sparkpost

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants