Note that there are some explanatory texts on larger screens.

plurals
  1. POConnect to GTalk server (XMPP, Smack) using an authToken
    primarykey
    data
    text
    <p>I'm writing a chat app which is connecting to a XMPP server, and if the user chooses, I want to give them the option to connect to their google chat account, without having to enter the credentials... I used google's javascript api to popout the google login form and after successful login access token will generate. Now using that access token and users email id i want to communicate with xmpp server so that users can chat with their gtalk friends.</p> <p>I searched a lot but didn't found the solution. Whatever i found required users password but i want to use access token. </p> <pre><code>SASLAuthentication.registerSASLMechanism("X-OAUTH2", GoogleConnectSASLMechanism.class); SASLAuthentication.supportSASLMechanism("X-OAUTH2", 0); config = new ConnectionConfiguration(server, 5222, 'gmail.com'); config.setSASLAuthenticationEnabled(true); config.setSecurityMode(SecurityMode.enabled); config.setReconnectionAllowed(true); connection = new XMPPConnection(config); connection.connect(); connection.login(username, session_key, "Chat"); setServer(SERVER_TYPE.GTALK); </code></pre> <p>GoogleConnectSASLMechanism.java code is as follows:-</p> <pre><code> package org.jivesoftware.smack; import java.io.IOException; import java.net.URLEncoder; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.GregorianCalendar; import java.util.HashMap; import java.util.Map; import javax.security.auth.callback.CallbackHandler; import javax.security.sasl.Sasl; import org.jivesoftware.smack.SASLAuthentication; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.packet.Packet; import org.jivesoftware.smack.sasl.SASLMechanism; import org.jivesoftware.smack.util.Base64; public class GoogleConnectSASLMechanism extends SASLMechanism { public static final String NAME="X-OAUTH2"; public GoogleConnectSASLMechanism(SASLAuthentication saslAuthentication) { super(saslAuthentication); } @Override protected String getName() { return NAME; } static void enable() { } @Override protected void authenticate() throws IOException, XMPPException { String authCode = password; String jidAndToken = "\0" + URLEncoder.encode( authenticationId, "utf-8" ) + "\0" + authCode; StringBuilder stanza = new StringBuilder(); //stanza.append( "&lt;auth mechanism=\"" ).append( getName() ); //stanza.append( "\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"&gt;" ); // stanza.append( new String(Base64.encode( jidAndToken.getBytes( "UTF-8" ), Base64.DEFAULT ) ) ); stanza.append( "&lt;auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\" mechanism=\"" ).append( getName() ); stanza.append( "\" auth:service=\"oauth2\"" ); stanza.append( "\" xmlns:auth=\"http://www.google.com/talk/protocol/auth\"&gt;" ); stanza.append( "\" base64(\"\\0"+user_name+"\\0" + authCode+")" ); stanza.append( "&lt;/auth&gt;" ); //Log.v("BlueTalk", "Authentication text is "+stanza); // Send the authentication to the server getSASLAuthentication().send( new Auth2Mechanism(stanza.toString()) ); } public class Auth2Mechanism extends Packet { String stanza; public Auth2Mechanism(String txt) { stanza = txt; } public String toXML() { return stanza; } } /** * Initiating SASL authentication by select a mechanism. */ public class AuthMechanism extends Packet { final private String name; final private String authenticationText; public AuthMechanism(String name, String authenticationText) { if (name == null) { throw new NullPointerException("SASL mechanism name shouldn't be null."); } this.name = name; this.authenticationText = authenticationText; } public String toXML() { StringBuilder stanza = new StringBuilder(); stanza.append("&lt;auth mechanism=\"").append(name); stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"&gt;"); if (authenticationText != null &amp;&amp; authenticationText.trim().length() &gt; 0) { stanza.append(authenticationText); } stanza.append("&lt;/auth&gt;"); return stanza.toString(); } } } </code></pre> <p>But I got an Exception from connection.login() that "username or password are not correct"</p> <p>To do this, I'd get the permission to use the google account, get the token and authenticate to google talk (XMPP server, using Smack) using the token..</p> <p>The problem is.. how do I do that? I mean, how do I authenticate to the GTalk server if I know the login and the token?</p> <p>Any help would be highly appreciated...:)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload