我正在使用 xmpp 框架将 gtalk 集成到我的应用程序中。我已经使用 OAuth 2.0 成功验证了用户。现在我想使用访问令牌和用户电子邮件来验证 xmpp 流。我知道使用此方法 authenticateWithPassword 会发送身份验证调用 xmppStreamDidConnect 方法。这需要密码,我想使用谷歌访问令牌来完成。有什么帮助吗?
1 回答
是的,您可以做到,请按照以下步骤操作:
- 在谷歌开发者控制台上注册您的应用程序。
生成具有以下范围的访问令牌:https: //www.googleapis.com/auth/googletalk
开始认证如下:
- (BOOL)start:(NSError **)errPtr { XMPPLogTrace();
// 来自 RFC 4616 - PLAIN SASL 机制:// [authzid] UTF8NUL authcid UTF8NUL passwd // // authzid:授权身份 // authcid:身份验证身份(用户名) // passwd:authcid 的密码
NSString *accessToken = @"ACCESS-TOKEN-STRING-FROM Google";//TODO: 分配你生成的访问令牌 NSLog(@"stream supports: %@",xmppStream.supportedAuthenticationMechanisms); NSString *payload = [NSString stringWithFormat:@"\0%@\0%@", xmppStream.hostName, accessToken]; NSLog(@"payload = %@",payload); NSString *base64 = [[payload dataUsingEncoding:NSUTF8StringEncoding] xmpp_base64Encoded]; NSXMLElement *auth = [NSXMLElement elementWithName:@"auth" xmlns:@"urn:ietf:params:xml:ns:xmpp-sasl"]; [auth addAttributeWithName:@"mechanism" stringValue:@"X-OAUTH2"]; [auth addAttributeWithName:@"auth:service" stringValue:@"oauth2"]; [auth addAttributeWithName:@"xmlns:auth" stringValue:@" https:
[xmppStream sendAuthElement:auth];
return YES;
}
一切都应该按预期工作,请发表评论。