Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9155833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:39:52+00:00 2026-06-17T12:39:52+00:00

I’m writing a chat app which is connecting to a XMPP server, and if

  • 0

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.

I searched a lot but didn’t found the solution. Whatever i found required users password but i want to use access token.

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);

GoogleConnectSASLMechanism.java code is as follows:-

    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( "<auth mechanism=\"" ).append( getName() );
        //stanza.append( "\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">" );
       // stanza.append( new String(Base64.encode( jidAndToken.getBytes( "UTF-8" ), Base64.DEFAULT ) ) );
        stanza.append( "<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\">" );
        stanza.append( "\" base64(\"\\0"+user_name+"\\0" + authCode+")" );



        stanza.append( "</auth>" );

        //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("<auth mechanism=\"").append(name);
            stanza.append("\" xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\">");
            if (authenticationText != null &&
                    authenticationText.trim().length() > 0) {
                stanza.append(authenticationText);
            }
            stanza.append("</auth>");
            return stanza.toString();
        }
        }
    }

But I got an Exception from connection.login() that “username or password are not correct”

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..

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?

Any help would be highly appreciated…:)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:39:53+00:00Added an answer on June 17, 2026 at 12:39 pm

    Vijay,

    Change your authenticate function as follows :

    protected void authenticate() throws IOException, XMPPException {
        final StringBuilder stanza = new StringBuilder();
        byte response[] = null;
    
        stanza.append("<auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"" +
                "mechanism=\"X-OAUTH2\"" +
                "auth:service=\"oauth2\"" +
                "xmlns:auth= \"http://www.google.com/talk/protocol/auth\">");
    
        String composedResponse =  "\0" + username + "\0" + sessionKey;
        response = composedResponse.getBytes("UTF-8");
        String authenticationText = "";
        if (response != null) {
            authenticationText = Base64.encodeBytes(response, Base64.DONT_BREAK_LINES);
        }
    
        stanza.append(authenticationText);
        stanza.append("</auth>");
    
        // Send the authentication to the server
        Packet p=new Packet() {
            @Override
            public String toXML() {
                return stanza.toString();
            }
        };
        getSASLAuthentication().send(p);
    }
    

    Its same as your original authenticate function. I have just changed stanza.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app for my school newspaper, which is run completely online
I am writing an app with both english and french support. The app requests
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
I'm interested in microtypography issues on the web. I want a tool to fix:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.