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

Authorization failed #68

Closed
GopherJ opened this issue Sep 21, 2020 · 6 comments
Closed

Authorization failed #68

GopherJ opened this issue Sep 21, 2020 · 6 comments

Comments

@GopherJ
Copy link
Contributor

GopherJ commented Sep 21, 2020

I made the same example using sendgrid-rs and sendgrid-nodejs

const sgMail = require("@sendgrid/mail");
sgMail.setApiKey(
    "xxxx"
);
const msg = {
    to: "alex_cj96@example.com",
    from: "cheng.jiang@example.com", // Use the email address or domain you verified above
    subject: "Sending with Twilio SendGrid is Fun",
    text: "and easy to do anywhere, even with Node.js",
    html: "<strong>and easy to do anywhere, even with Node.js</strong>",
};
//ES6
sgMail.send(msg).then(
    () => {},
    error => {
        console.error(error);

        if (error.response) {
            console.error(error.response.body);
        }
    }
)
//ES8
;(async () => {
    try {
        await sgMail.send(msg);
    } catch (error) {
        console.error(error);

        if (error.response) {
            console.error(error.response.body);
        }
    }
})();
use std::collections::HashMap;

use sendgrid::errors::SendgridError;
use sendgrid::v3::*;

#[tokio::main]
async fn main() -> Result<(), ()> {
    println!("Hello, world!");
    let mut cool_header = HashMap::new();
    cool_header.insert(String::from("x-cool"), String::from("indeed"));
    cool_header.insert(String::from("x-cooler"), String::from("cold"));

    let p = Personalization::new()
        .add_to(Email::new().set_email("alex_cj96@example.com"))
        .add_headers(cool_header);

    let m = Message::new()
        .set_from(Email::new().set_email("cheng.jiang@example.com"))
        .set_subject("Subject")
        .add_content(
            Content::new()
                .set_content_type("text/html")
                .set_value("Test"),
        )
        .add_personalization(p);

    // let mut env_vars = ::std::env::vars();
    // let api_key = env_vars.find(|v| v.0 == "SG_API_KEY").unwrap();
    let sender = Sender::new(
"xxxx"
    );
    let resp = sender.send(&m).await.unwrap();
    println!("status: {}", resp.status());
    Ok(())
}
@GopherJ
Copy link
Contributor Author

GopherJ commented Sep 21, 2020

sendgrid-rs failed but sendgrid nodejs succeed

@GopherJ
Copy link
Contributor Author

GopherJ commented Sep 21, 2020

error message:

Hello, world!
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: RequestNotSuccessful(401, "{\"errors\":[{\"message\":\"The provided authorization grant is invalid, expired, or revoked\",\"field\":null,\"help\":null}]}")', src/main.rs:32:38
stack backtrace:

@GopherJ
Copy link
Contributor Author

GopherJ commented Sep 21, 2020

@gsquire any idea on this?

@gsquire
Copy link
Owner

gsquire commented Sep 21, 2020

It looks like you commented out the API key loading from the environment variables.

@GopherJ
Copy link
Contributor Author

GopherJ commented Sep 21, 2020

@gsquire I seem figured it out, I'm probably having sendgrid v1 or v2 api key, I'll test it soon

@GopherJ
Copy link
Contributor Author

GopherJ commented Sep 21, 2020

@gsquire No sorry my api key is v3, I just confirmed and I found an extra space in my api key, now it's working.... :)

@GopherJ GopherJ closed this as completed Sep 21, 2020
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

2 participants