通过 Google API 发送电子邮件
Send email by Google API
我正在尝试使用 Google API
发送电子邮件
发送电子邮件控制器看起来像
public function sendMessage()
{
$client = self::getClient();
$service = new Google_Service_Gmail($client);
$mailer = $service->users_messages;
$message = (new \Swift_Message('Here is my subject'))
->setFrom('myemailaddress@myserver.com')
->setTo(['receiver@someserver.com' => 'Test Name'])
->setContentType('text/html')
->setCharset('utf-8')
->setBody('<h4>Here is my body</h4>');
$msg_base64 = (new \Swift_Mime_ContentEncoder_Base64ContentEncoder())
->encodeString($message->toString());
$message = new Google_Service_Gmail_Message();
$message->setRaw($msg_base64);
$message = $mailer->send('me', $message);
print_r($message);
}
获取客户端class:
function getClient()
{
$client = new Google_Client();
$client->setRedirectUri('http://' . 'site.com' . '/oauth2callback.php');
$client->setApplicationName('Gmail API PHP');
$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
return $client;
}
当我尝试 运行 时收到错误消息:
{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Login Required.", "domain": "global", "reason": "required", "location": "Authorization", "locationType": "header" } ], "status": "UNAUTHENTICATED" } }
credentials.json
{"web":{
"client_id":"REDACTED",
"project_id":"project-44",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"redacted",
"access_token":"redacted"}}
我的代码可能有什么问题?当我试图打印出 $client 时,它会显示所需的数据。或者我应该如何登录才能正确使用它而不会受到干扰?我之前登录过(几个小时前)。
您似乎错过了获取 and/or 设置访问令牌或刷新令牌的步骤。在 PHP Quickstart for Gmail 中,是这段代码:
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
稍微分解一下:
- 您需要通过将用户发送到授权 URL (
$client->createAuthUrl()
) 来请求用户授权
- 然后用代码交换访问令牌 (
$client->fetchAccessTokenWithAuthCode($authCode)
)
- 并在您的客户端上设置 (
$client->setAccessToken($accessToken)
) 或保存以备将来使用。
除了访问令牌,您还需要保存返回的刷新令牌,这样您就不必每次都重新登录 ($client->fetchAccessTokenWithRefreshToken($refreshToken)
)。如果您已经完成此过程,则可能是您的访问令牌已过期,您需要使用刷新令牌或重新验证才能再次获得访问权限。
如果您正在为更多用户开发此应用程序,而不仅仅是您自己,您可能需要查看我工作的 managed OAuth platforms like Xkit。他们处理获取授权、刷新令牌等的过程,以及加密令牌并为每个用户存储它们。
我正在尝试使用 Google API
发送电子邮件发送电子邮件控制器看起来像
public function sendMessage()
{
$client = self::getClient();
$service = new Google_Service_Gmail($client);
$mailer = $service->users_messages;
$message = (new \Swift_Message('Here is my subject'))
->setFrom('myemailaddress@myserver.com')
->setTo(['receiver@someserver.com' => 'Test Name'])
->setContentType('text/html')
->setCharset('utf-8')
->setBody('<h4>Here is my body</h4>');
$msg_base64 = (new \Swift_Mime_ContentEncoder_Base64ContentEncoder())
->encodeString($message->toString());
$message = new Google_Service_Gmail_Message();
$message->setRaw($msg_base64);
$message = $mailer->send('me', $message);
print_r($message);
}
获取客户端class:
function getClient()
{
$client = new Google_Client();
$client->setRedirectUri('http://' . 'site.com' . '/oauth2callback.php');
$client->setApplicationName('Gmail API PHP');
$client->setScopes(Google_Service_Gmail::GMAIL_READONLY);
$client->setAuthConfig('credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
return $client;
}
当我尝试 运行 时收到错误消息:
{ "error": { "code": 401, "message": "Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.", "errors": [ { "message": "Login Required.", "domain": "global", "reason": "required", "location": "Authorization", "locationType": "header" } ], "status": "UNAUTHENTICATED" } }
credentials.json
{"web":{
"client_id":"REDACTED",
"project_id":"project-44",
"auth_uri":"https://accounts.google.com/o/oauth2/auth",
"token_uri":"https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs",
"client_secret":"redacted",
"access_token":"redacted"}}
我的代码可能有什么问题?当我试图打印出 $client 时,它会显示所需的数据。或者我应该如何登录才能正确使用它而不会受到干扰?我之前登录过(几个小时前)。
您似乎错过了获取 and/or 设置访问令牌或刷新令牌的步骤。在 PHP Quickstart for Gmail 中,是这段代码:
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = 'token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
稍微分解一下:
- 您需要通过将用户发送到授权 URL (
$client->createAuthUrl()
) 来请求用户授权
- 然后用代码交换访问令牌 (
$client->fetchAccessTokenWithAuthCode($authCode)
) - 并在您的客户端上设置 (
$client->setAccessToken($accessToken)
) 或保存以备将来使用。
除了访问令牌,您还需要保存返回的刷新令牌,这样您就不必每次都重新登录 ($client->fetchAccessTokenWithRefreshToken($refreshToken)
)。如果您已经完成此过程,则可能是您的访问令牌已过期,您需要使用刷新令牌或重新验证才能再次获得访问权限。
如果您正在为更多用户开发此应用程序,而不仅仅是您自己,您可能需要查看我工作的 managed OAuth platforms like Xkit。他们处理获取授权、刷新令牌等的过程,以及加密令牌并为每个用户存储它们。