libcurl:无法使用smtp和oauth2通过gmail进行身份验证

时间:2014-06-06 01:56:02

标签: c++ smtp oauth-2.0 gmail libcurl

我正在尝试使用libcurl,smtp和oauth2通过gmail发送电子邮件。我已经成功获得了access_token,并且已经使用

进行了检查
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token={access_token}

确保它实际上是一个有效的access_token。但是,每当我尝试验证并发送电子邮件时,它总是抱怨“身份验证失败:334”。当我用密码行替换oauth2行时,代码工作正常。

以下是代码:

static const char *payload_text[] = {
  "Date: Mon, 29 Nov 2010 21:54:29 +1100\r\n",
  "To: XXX@gmail.com\r\n",
  "From: XXX@gmail.com\r\n",
  "Subject: SMTP example message\r\n",
  "\r\n", /* empty line to divide headers from body, see RFC5322 */ 
  "The body of the message starts here.\r\n",
  "\r\n",
  "It could be a lot of lines, could be MIME encoded, whatever.\r\n",
  "Check RFC5322.\r\n",
  NULL
};

struct upload_status {
  int lines_read;
};

static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) {
  struct upload_status *upload_ctx = (struct upload_status *)userp;
  const char *data;

  if ((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
    return 0;
  }

  data = payload_text[upload_ctx->lines_read];

  if(data) {
    size_t len = strlen(data);
    memcpy(ptr, data, len);
    upload_ctx->lines_read++;

    return len;
  }

  return 0;
}

void send_email() {
  CURL *curl;
  CURLcode res = CURLE_OK;
  struct curl_slist *recipients = NULL;
  struct upload_status upload_ctx;

  upload_ctx.lines_read = 0;

  curl = curl_easy_init();
  if(curl) {
    curl_easy_setopt(curl, CURLOPT_USERNAME, "XXX@gmail.com");
    curl_easy_setopt(curl, CURLOPT_XOAUTH2_BEARER, access_token.c_str());
    curl_easy_setopt(curl, CURLOPT_URL, "smtp://smtp.gmail.com:587/");
    curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL);
    curl_easy_setopt(curl, CURLOPT_MAIL_FROM, "XXX@gmail.com");
    recipients = curl_slist_append(recipients, "XXXX@gmail.com");
    curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
    curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
    curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
    curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
    curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);

    res = curl_easy_perform(curl);

    if (res != CURLE_OK) 
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
          curl_easy_strerror(res));

    curl_slist_free_all(recipients);
    curl_easy_cleanup(curl);
  }
}

,这是错误消息的后半部分(不同之处并显示失败):

< 250-mx.google.com at your service, [108.200.141.166]
< 250-SIZE 35882577
< 250-8BITMIME
< 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
< 250-ENHANCEDSTATUSCODES
< 250 CHUNKING
> AUTH XOAUTH2
< 334 
> dXNlcj1yY3dtY3Njcm9vZ2VAZ21haWwuY29tAWF1dGg9QmVhcmVyIHlhMjkuS2dCUVhCU1ZzSVpCQ1I4QUFBQzVucmdES2g3TzBQZ01JU25zRGlGSjc0c1M2N0Z5cUQycUJrVkgzdHFrV3cBAQ==
< 334 eyJzdGF0dXMiOiI0MDAiLCJzY2hlbWVzIjoiQmVhcmVyIiwic2NvcGUiOiJodHRwczovL21haWwuZ29vZ2xlLmNvbS8ifQ==
* Authentication failed: 334
* Closing connection 0
curl_easy_perform() failed: Login denied

任何人都可以帮忙解释这里出了什么问题吗?更糟糕的是,我最终会使用用户名/密码组合登录,但我宁愿弄清楚这里搞砸了什么(而且我的印象是oauth2方式是首选方式)

提前感谢您的帮助。

0 个答案:

没有答案