【问题标题】:Xmpp gtalk authentication with google access token使用谷歌访问令牌进行 Xmpp gtalk 身份验证
【发布时间】:2013-07-05 13:07:38
【问题描述】:
我正在使用 xmpp 框架将 gtalk 集成到我的应用程序中。我已经使用 OAuth 2.0 成功验证了用户。现在我想使用访问令牌和用户电子邮件来验证 xmpp 流。我知道使用此方法authenticateWithPassword 发送身份验证调用xmppStreamDidConnect 方法。这需要密码,我想使用谷歌访问令牌来完成。有什么帮助吗?
【问题讨论】:
标签:
iphone
ios
xmpp
google-talk
【解决方案1】:
是的,您可以做到,请按照以下步骤操作:
- 在 Google 开发者控制台上注册您的应用。
生成具有以下范围的访问令牌:
https://www.googleapis.com/auth/googletalk
-
如下开始认证:
- (BOOL)开始:(NSError **)errPtr
{
XMPPLogTrace();
// 来自 RFC 4616 - PLAIN SASL 机制:
// [authzid] UTF8NUL authcid UTF8NUL 密码
//
// authzid:授权标识
// authcid:认证身份(用户名)
// passwd : authcid 的密码
NSString *accessToken = @"ACCESS-TOKEN-STRING-FROM Google";//TODO: 分配你生成的访问令牌
NSLog(@"流支持:%@",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://www.google.com/talk/protocol/auth"];
[auth setStringValue:base64];
[xmppStream sendAuthElement:auth];
return YES;
}
一切都应该按预期工作,请发表评论。