使用谷歌访问令牌的Xmpp gtalk身份验证

问题描述:

我正在使用xmpp框架将gtalk集成到我的应用程序中。我已成功使用OAuth 2.0对用户进行身份验证。现在我想使用访问令牌和用户电子邮件来验证xmpp流。我知道认证调用使用此方法authenticateWithPassword发送xmppStreamDidConnect方法。这需要一个密码,我想使用谷歌访问令牌来完成它。任何帮助?使用谷歌访问令牌的Xmpp gtalk身份验证

是的,你可以做到这一点,请按照下列步骤:

  1. 注册您的谷歌开发者控制台上的应用程序。
  2. 生成访问令牌以下列范围: https://www.googleapis.com/auth/googletalk

  3. 开始验证如下:

    • (BOOL)开始:(NSError **)errPtr { XMPPLogTrace();

    //从RFC 4616 - PLAIN SASL机制: // [authzid] UTF8NUL authcid UTF8NUL passwd的 // // authzid:授权身份 // authcid:认证标识(用户名) // passwd的:密码authcid

的NSString *的accessToken = @ “ACCESS-TOKEN串,从谷歌”; // 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; 

}

一切都应该按预期工作,请评论。