Ios 如何在objective c中使用oauth身份验证而不是用户名和密码发送电子邮件
我正在使用一个iPhone应用程序,该应用程序必须允许自动发送电子邮件,而无需任何用户交互(我不能使用MFMailComposeViewController,也不能将用户的用户名/密码存储在像SKPSMTP或MailCore这样的SMTP库中)。 我的想法是,允许用户使用OAuth协议使用他的Gmail帐户登录,然后,在其他时刻,使用之前提供的access_令牌发送电子邮件,而无需重新验证或要求用户提供任何附加数据。 我正在使用GoogleOpenSource.framework和GooglePlus.framework登录用户。 任何建议都会有帮助。Ios 如何在objective c中使用oauth身份验证而不是用户名和密码发送电子邮件,ios,objective-c,oauth,smtp,Ios,Objective C,Oauth,Smtp,我正在使用一个iPhone应用程序,该应用程序必须允许自动发送电子邮件,而无需任何用户交互(我不能使用MFMailComposeViewController,也不能将用户的用户名/密码存储在像SKPSMTP或MailCore这样的SMTP库中)。 我的想法是,允许用户使用OAuth协议使用他的Gmail帐户登录,然后,在其他时刻,使用之前提供的access_令牌发送电子邮件,而无需重新验证或要求用户提供任何附加数据。 我正在使用GoogleOpenSource.framework和GoogleP
提前感谢。您应该能够通过此smtp命令使用OAUTH令牌对GMAIL smtp服务器进行身份验证:
AUTH XOAUTH <your OAUTH token>
我不熟悉iPhone的开发,因此无法向您提供一些示例代码。但是您需要一个支持扩展身份验证方案的SMTP库。最后,我找到了解决问题的方法。接下来,我将详细介绍使用OAuth身份验证通过google发送电子邮件的步骤: 对于OAuth登录,我使用了GooglePlus.framework中包含的GPPSignIn类。对于那些可能感兴趣的人,只需按照列出的说明进行操作。 在登录窗口中,完成流程后,将调用下一个委托方法:
- (void)finishedWithAuth: (GTMOAuth2Authentication *)auth error: (NSError *) error {
NSLog(@"Received error %@ and auth object %@",error, auth);
if(error) {
NSLog(@"%@",[error description]);
}
else {
// Get the refresh token.
self.refreshToken = auth.refreshToken;
// If you need the user's email you must set the permissions on the GPPSignIn instance.
// store it locally for future uses.
self.email = [GPPSignIn sharedInstance].userEmail;
}
}
然后,为了发送电子邮件,我使用了MailCore的库。
当您希望使用OAuth accessToken发送电子邮件时,首先必须使用您以前在登录过程中保存的刷新令牌获取更新的令牌:
- (void)sendMail {
GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:GOOGLE_OAUTH_KEYCHAIN clientID:GOOGLE_CLIENT_ID clientSecret:GOOGLE_CLIENT_SECRET];
//I use the saved refreshToken
auth.refreshToken = refreshToken;
[auth beginTokenFetchWithDelegate:self
didFinishSelector:@selector(auth:finishedRefreshWithFetcher:error:)];
}
- (void)auth:(GTMOAuth2Authentication *)auth finishedRefreshWithFetcher:(GTMHTTPFetcher *)fetcher error:(NSError *)error {
if (error != nil) {
NSLog(@"Authentication failed");
return;
}
NSString * email = storedEmail;
NSString * accessToken = [auth accessToken];
MCOSMTPSession * smtpSession = [[MCOSMTPSession alloc] init];
smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = email; //saved value
smtpSession.connectionType = MCOConnectionTypeTLS;
smtpSession.password = nil; //nil
smtpSession.OAuth2Token = accessToken; //saved value
smtpSession.authType = MCOAuthTypeXOAuth2;
MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
MCOAddress *fromAddress = [MCOAddress addressWithMailbox:email];
MCOAddress *toAddress = [MCOAddress addressWithMailbox:recipientEmail];
[[builder header] setFrom:fromAddress];
[[builder header] setTo:toAddresses];
[[builder header] setSubject:@"Some subject"];
[builder setHTMLBody:@"some message"];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"%@ Error sending email:%@", email, error);
} else {
NSLog(@"%@ Successfully sent email!", email);
}
}];
}
- (void)sendMail {
GTMOAuth2Authentication * auth = [GTMOAuth2ViewControllerTouch authForGoogleFromKeychainForName:GOOGLE_OAUTH_KEYCHAIN clientID:GOOGLE_CLIENT_ID clientSecret:GOOGLE_CLIENT_SECRET];
//I use the saved refreshToken
auth.refreshToken = refreshToken;
[auth beginTokenFetchWithDelegate:self
didFinishSelector:@selector(auth:finishedRefreshWithFetcher:error:)];
}
- (void)auth:(GTMOAuth2Authentication *)auth finishedRefreshWithFetcher:(GTMHTTPFetcher *)fetcher error:(NSError *)error {
if (error != nil) {
NSLog(@"Authentication failed");
return;
}
NSString * email = storedEmail;
NSString * accessToken = [auth accessToken];
MCOSMTPSession * smtpSession = [[MCOSMTPSession alloc] init];
smtpSession = [[MCOSMTPSession alloc] init];
smtpSession.hostname = @"smtp.gmail.com";
smtpSession.port = 465;
smtpSession.username = email; //saved value
smtpSession.connectionType = MCOConnectionTypeTLS;
smtpSession.password = nil; //nil
smtpSession.OAuth2Token = accessToken; //saved value
smtpSession.authType = MCOAuthTypeXOAuth2;
MCOMessageBuilder * builder = [[MCOMessageBuilder alloc] init];
MCOAddress *fromAddress = [MCOAddress addressWithMailbox:email];
MCOAddress *toAddress = [MCOAddress addressWithMailbox:recipientEmail];
[[builder header] setFrom:fromAddress];
[[builder header] setTo:toAddresses];
[[builder header] setSubject:@"Some subject"];
[builder setHTMLBody:@"some message"];
NSData * rfc822Data = [builder data];
MCOSMTPSendOperation *sendOperation = [smtpSession sendOperationWithData:rfc822Data];
[sendOperation start:^(NSError *error) {
if(error) {
NSLog(@"%@ Error sending email:%@", email, error);
} else {
NSLog(@"%@ Successfully sent email!", email);
}
}];
}