2013-07-05 73 views
0

我正在使用xmpp框架將gtalk集成到我的應用程序中。我已成功使用OAuth 2.0對用戶進行身份驗證。現在我想使用訪問令牌和用戶電子郵件來驗證xmpp流。我知道認證調用使用此方法authenticateWithPassword發送xmppStreamDidConnect方法。這需要一個密碼,我想使用谷歌訪問令牌來完成它。任何幫助?使用谷歌訪問令牌的Xmpp gtalk身份驗證

回答

1

是的,你可以做到這一點,請按照下列步驟:

  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; 

}

一切都應該按預期工作,請評論。