2017-02-13 6 views
0

下記の私の試行で示されているように、oriceon/oauth-5-laravel packageでLaravel 5.2を使用しようとしています。誰かが私のウェブサイトから自分のアカウントを盗んでしまいます。私は応答していLaravel 5.2 swift_messageによって作成されたメールをoriceon/oauth-5-laravel経由で送信する

Attempt:1 using laravels inbuilt swift_message(note emails are just examples) 
$message = Swift_Message::newInstance(); 
$message->setSubject("Test Email"); 
$message->setFrom("[email protected]"); 
$message->setTo("[email protected]"); 
$message->setReplyTo("[email protected]"); 
$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"]= "[email protected]"; 
$envelope["to"] = "[email protected]"; 
$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」を持っていると信じています。私はまた、送信元アドレスが承認されたユーザーの電子メールアドレスであることを確認しましたが、私が何かばかげたことをしているか、エンコーディングで何かを見逃しているのかどうかはわかりません。

何か助けがあれば、2日間これでこれに固執しているでしょう!

+0

リクエストの作成方法を確認したい場合があります。エラー400があります:リクエストが間違っている可能性があります。リクエストフォーマットが間違っているため、Google Serverに渡されるパラメータが不十分です。認証がどのようにしてリクエストにエラー400を返す可能性があるかを説明した関連するSO [post](http://stackoverflow.com/a/32667951/5995040)をチェックするとよいでしょう。コードの実装によっては、 [PHPクイックスタート](https://developers.google.com/gmail/api/quickstart/php)お役に立てれば。 –

+0

これは不正な形式のリクエストだと言っているようです。 私がもっと知る必要があるのは、ここにあるドキュメント[リンク](https://developers.google.com/gmail/api/v1/reference/users/messages)のように、適切なリクエストのフォーマットは何ですか?/send)は、投稿要求でUsers.messagesリソースを渡す必要があると述べています。 ドキュメントで見たことから、正しいパラメータを持つjsonオブジェクトのように見えます。私は上のようにリンクからこの例を模倣しようとしましたが、私はidの等を手動で設定する必要があるかどうかはわかりません。 –

+0

私は実用的なソリューションを持っています!ここに投稿するだけで、誰かがこの問題に似たようなことに似ています。 –

答えて

2

実際のメッセージを送信するには、googleのgoogle/apiclientパッケージと一緒にoriceon/oauth-5-laravelパッケージでLaravel 5.2を使用してください。ガイダンスのために@ 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 = '[email protected]'; 
     // 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); 
    } 
関連する問題