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?
1 Answers
Yes you can do it please follow the steps:
- Register your app on google developer console.
Generate access token with following scope: https://www.googleapis.com/auth/googletalk
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.