title: Gtalk XMPP SASL authentication failed using mechanism X-OAUTH2?
body: <p>I am using GoogleTalk XMPP in my application for chatting. unable to create XMPP connection by using username and AuthToken with <code>Google authentication</code>. </p>
  
  <p>Now i am using <code>GoogleAuth2</code> for <code>authentication</code>. i tried to authenticate like this using access_token and email. by using <code>SASLMechanism</code>. but i am not able to connect to xmpp serv
  er, it gives error like this <code>SASL authentication failed using mechanism X-OAUTH2</code></p>
  
  <pre><code>ConnectionConfiguration config = new ConnectionConfiguration(server_host, SERVER_PORT, SERVICE_NAME);
  config.setSASLAuthenticationEnabled(true);
  m_connection = new XMPPConnection(config);
  
  SASLAuthentication.registerSASLMechanism("X-OAUTH2", GoogleConnectSASLMechanism.class);
  SASLAuthentication.supportSASLMechanism("X-OAUTH2", 0);
  
  config.setSecurityMode(ConnectionConfiguration.SecurityMode.enabled);   
  try {           
      m_connection.connect();     
  } catch (XMPPException e) {         
      e.printStackTrace();
  }
  </code></pre>
  
  <p>this is the class i am using for <code>SASLMechanism.</code></p>
  
  <pre><code>public class GoogleConnectSASLMechanism extends SASLMechanism {
  
  public static final String NAME = "X-OAUTH2";
  
  private String username = "";
  private String sessionKey = "";
  
  public GoogleConnectSASLMechanism(SASLAuthentication saslAuthentication) {
      super(saslAuthentication);
  }
  
  @Override
  protected String getName() {
      return NAME;
  }
  
  static void enable() {
  }
  
  @Override
  protected void authenticate() throws IOException, XMPPException {
  
      final StringBuilder stanza = new StringBuilder();
      byte response[] = null;
  
      stanza.append("&lt;auth xmlns=\"urn:ietf:params:xml:ns:xmpp-sasl\"" +
              "mechanism=\"X-OAUTH2\"" +
              "auth:service=\"oauth2\"" +
              "xmlns:auth= \"http://www.google.com/talk/protocol/auth\"&gt;");
  
      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("&lt;/auth&gt;");
  
      // Send the authentication to the server
      Packet p=new Packet() {
          @Override
          public String toXML() {
              return stanza.toString();
          }
      };
      getSASLAuthentication().send(p);
  }
  
  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>How to authenticate the <code>Google Auth</code> by using SASL Mechanism ?</p>