【问题标题】:Laravel 5.2 sending emails created by swift_message via oriceon/oauth-5-laravelLaravel 5.2 通过 oriceon/oauth-5-laravel 发送由 swift_message 创建的电子邮件
【发布时间】:2017-07-01 22:53:36
【问题描述】:

如下面的尝试所示,我正在尝试使用 Laravel 5.2 和 oriceon/oauth-5-laravel package 来授权我网站上的用户,然后允许该用户通过我的网站通过他们自己的帐户向其他人发送电子邮件。

Attempt:1 using laravels inbuilt swift_message(note emails are just examples)
$message = Swift_Message::newInstance();
$message->setSubject("Test Email");
$message->setFrom("dukeforsythtester@gmail.com");
$message->setTo("tester@gmail.com");
$message->setReplyTo("dukeforsythtester@gmail.com");
$message->setBody("This is a test of adding a body!");
$to_send = rtrim(strtr(base64_encode($message->toString()), '+/', '-_'),'=');
$result = $googleService->request('https://www.googleapis.com/gmail/v1/users/me/messages/send',$to_send);


Attempt 2: using imap_mail_compose via php natively
$envelope["from"]= "dukeforsythtester@gmail.com"; 
$envelope["to"]  = "dukeforsyth@outlook.com";
$envelope["subject"]  = "Testing...";
$part1["type"] = TYPETEXT;
$part1["subtype"] = "plain";
$part1["description"] = "description3";
$part1["contents.data"] = "contents.data3\n\n\n\t";

$body[1] = $part1;

$mime = imap_mail_compose ($envelope , $body);
$mime = rtrim(strtr(base64_encode($mime), '+/', '-_'), '=');
$result = $googleService->request('https://www.googleapis.com/gmail/v1/users/me/messages/send',$mime);

无论哪种方式,我都会得到以下回应:

TokenResponseException in StreamClient.php line 68:
Failed to request resource. HTTP Code: HTTP/1.1 400 Bad Request

我相信我有正确的范围“https://www.googleapis.com/auth/gmail.send”在用户授权后为用户发送电子邮件。我还确保发件人地址是授权用户的电子邮件地址,但我不确定我是在做一些愚蠢的事情还是在编码中遗漏了一些东西。

任何帮助将不胜感激,现在已经坚持了两天!

【问题讨论】:

  • 您可能想要检查您是如何撰写请求的。出现错误 400 : Bad Request 可能是由于请求格式错误、传递给 Google 服务器的所需参数不足。您可能需要查看此相关的 SO post,其中讨论了身份验证如何使请求返回错误 400。对于某些代码实现,您可能需要查看 Google 的 PHP QuickStart。希望这会有所帮助。
  • 就像你说的,这是一个格式错误的请求。我需要更多了解的是正确请求的格式是什么,就像在link 的文档中一样,它指出在发布请求中我需要传递一个 Users.messages 资源。从我在文档中看到的内容来看,这看起来像是一个带有正确参数的 json 对象。我试图从上面显示的链接中模仿这个示例,但我不确定是否必须手动设置 id 等。
  • 我有一个可行的解决方案!在这里发帖以防有人遇到此问题或类似问题!

标签: php oauth laravel-5.2 gmail-api swiftmailer


【解决方案1】:

这是我使用 Laravel 5.2 和 oriceon/oauth-5-laravel 包以及谷歌的 google/apiclient 包发送实际消息的解决方案。非常感谢@Mr.Rebot 的指导!

    $code = Input::get('code');
    $googleService = \OAuth::consumer('Google');
    // if code is provided get user data and sign in
    if ( ! is_null($code) ) 
    { 
        // Variables
        $redirect_uri = 'your redirect url that is set on google console';
        $user_to_impersonate = 'dukeforsythtester@gmail.com';
        // Create client with these set credentials
        $client = new \Google_Client();
        $client->setAuthConfig('path to your json given by google console');
        $client->setRedirectUri($redirect_uri);
        $client->setSubject($user_to_impersonate);
        $scopes = [ \Google_Service_Gmail::GMAIL_SEND ];
        $client->setScopes($scopes);

        $token = $client->authenticate($code);
        $client->setAccessToken($token);        
        if( $client->isAccessTokenExpired() )
        {
            // need to refresh token
            $refreshToken = $client->refreshToken($token);
            $client->setAccessToken($refreshToken);

        }

        // Create the service
        $service = new \Google_Service_Gmail($client);

        // Create the message
        $msg = new \Google_Service_Gmail_Message();
        $msg->setRaw($this->create_message());

        // Send off the message
        $this->sendMessage($service, 'me', $msg);
    }
    else {
        // Get googleService authorization
        $url = $googleService->getAuthorizationUri();
        // return to google login url
        return redirect((string)$url);
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-01
    • 1970-01-01
    • 2018-03-21
    • 2018-01-21
    • 1970-01-01
    • 2013-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多