简体   繁体   中英

Why do I get 403 error only on certain apis on SendGrid Api v3?

I am experiencing a strange problem on using SendGrid APIs. Basically, there are certain API calls that I can perform, and some other returns a 403 error.

I can send single emails:

$email = new \SendGrid\Mail\Mail(); 
    $email->setFrom("myfrom@myurl.net", "NAMEFROM");
    $email->setSubject("Sending with SendGrid is Fun");
    $email->addTo("myfrom@myurl.net", "NAMEFROM");
    $email->addContent("text/plain", "and easy to do anywhere, even with PHP");
    $email->addContent(
        "text/html", "<strong>and easy to do anywhere, even with PHP</strong>"
    );
    $sendgrid = new \SendGrid(getenv('SENDGRID_API_KEY'));
    try {
        $response = $sendgrid->send($email);
        print $response->statusCode() . "\n";
        print_r($response->headers());
        print $response->body() . "\n";
    } catch (Exception $e) {
        echo 'Caught exception: '. $e->getMessage() ."\n";
    }

But I am not able to send massive emails as campaigns:

$request_body = json_decode('{
        "id": <DATA HIDDEN>,
        "title": "May Newsletter",
        "subject": "New Products for Summer!",
        "sender_id": <DATA HIDDEN>,
        "list_ids": [
            "<DATA HIDDEN>"
        ],
        "html_content": "<html><head><title></title></head><body><p>Check out our summer line!</p></body></html>",
        "plain_content": "Check out our summer line!",
    }');
    $sg = new SendGrid(config('services.extra.SENDGRID_API_KEY')); 
    $response = $sg->client->campaigns()->post($request_body);

This call returns 403, "message":"access forbidden". Without any other indication.

Even the API Calls that follow:

https://api.sendgrid.com/v3/senders/<data hidden>
https://api.sendgrid.com/v3/senders

return 403.

I have a free plan and a verified email address (the "from address"). All the tokens I have generated have the "full" permission set.

I haven't read anything about the accessibility of an API regarding the price plan (free/paid) in the docs, but still, I wonder if anybody knows anything.

Any help is appreciated.

The Email API and Marketing Campaigns are two different products (see the two different tabs on the pricing page ). You can use both within one account, you just need to activate both products. I think you need to go to your account settings and activate the Marketing Campaigns product and then your API requests will succeed.

When i create the API key, it creates it with quite restricted permissions. You then need to go and enable the permissions on the key. I was getting 403 errors consistently, and when I finally added the permissions for the part of the API I was using, they started working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM