Вопрос

I am using xmpp framework to integrate gtalk in my application. I have successfully authenticated user using OAuth 2.0. Now i want to use the access token and user email to authenticate xmpp stream. I know that the authentication call get sent xmppStreamDidConnect method using this method authenticateWithPassword. This requires a password, i want to get it done using the google access token. Any help?

Это было полезно?

Решение

Yes you can do it please follow the steps:

  1. Register your app on google developer console.
  2. Generate access token with following scope: https://www.googleapis.com/auth/googletalk

  3. start authentication as following:

    • (BOOL)start:(NSError **)errPtr { XMPPLogTrace();

    // From RFC 4616 - PLAIN SASL Mechanism: // [authzid] UTF8NUL authcid UTF8NUL passwd // // authzid: authorization identity // authcid: authentication identity (username) // passwd : password for authcid

NSString *accessToken = @"ACCESS-TOKEN-STRING-FROM Google";//TODO: assign your generated access token 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://www.google.com/talk/protocol/auth"]; [auth setStringValue:base64];

[xmppStream sendAuthElement:auth];

return YES;

}

Everything should work as expected, please comment.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top