I want to send an email using a particular email address. But in my app first I sign in using google and then send emails using Gmail API using logged in user auth-headers.
But in this, anyone can log in and send emails. but I want only a particular email can send emails from my app. So I statically store auth headers for that particular email and sending email but my problem is that auth headers is expire after some time.
Here my code for google sign In:
GoogleSignIn googleSignIn = new GoogleSignIn(
scopes: <String>['https://www.googleapis.com/auth/gmail.send'],
);
await googleSignIn.signIn().then((data) async {
await data.authHeaders.then((result) async {
var header = {
'Authorization': result['Authorization'],
'X-Goog-AuthUser': result['X-Goog-AuthUser']
};
await testingEmail(data.email, header);
});
});
Code for send email:
Future<Null> testingEmail(String userId, Map header) async {
header['Content-type'] = 'application/json';
var from = userId;
var to = 'email in which send email';
var subject = 'subject of mail';
var message = 'Body of email';
var content = '''
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
to: $to
from: 'Email alias name <$from>'
subject: $subject
$message''';
var bytes = utf8.encode(content);
var base64 = base64Encode(bytes);
var body = json.encode({'raw': base64});
String url = 'https://www.googleapis.com/gmail/v1/users/' +
userId +
'/messages/send';
final http.Response response =
await http.post(url, headers: header, body: body);
if (response.statusCode != 200) {
return Future.error("Something went wrong");
}
}
Help me to sort out this problem