SASLError using PLAIN: not-authorized
Ok, that was rather a silly mistake. The current code is perfectly fine. I accidentally put username in place of password. I got to know about the error when I tried to go back to aSmack.
For authorization policy,
PLAIN Auth - Unblacklist PLAIN, Blacklist SHA-1 AND MD5
MD5 Auth - Unblacklist MD5, Blacklist SHA-1 AND PLAIN
SCRAM-SHA-1 Auth - Unblacklist SCRAM-SHA-1 & PLAIN, Blacklist MD5
SASLAuthentication.unBlacklistSASLMechanism("AuthName");
SASLAuthentication.blacklistSASLMechanism("AuthName");
AuthNames : PLAIN
, SCRAM-SHA-1
, MD5
** Check at server side that which auth method enabled
I was facing the same error.
After connecting to xmpp server I was calling
mConnection.login("[email protected]", "ilink@2012");
and it was not working.
To solve the issue I have done the following changes in my code.
SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
mConnection.login("test", "ilink@2012");
I have added two lines which blacklist DIGEST-MD5 and enable PLAIN SASLMechanism and also removed IP address from username.
For your reference here is my complete working code.
package com.ilink.xmpptest;
import java.io.IOException;
import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.ConnectionListener;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.SmackException;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity implements ConnectionListener {
private static final String TAG = MainActivity.class.getSimpleName();
private AbstractXMPPConnection mConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new ConnectToXmppServer().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void authenticated(XMPPConnection arg0, boolean arg1) {
Log.i(TAG, "Authenticated");
}
@Override
public void connected(XMPPConnection arg0) {
Log.i(TAG, "Connected");
try {
SASLAuthentication.unBlacklistSASLMechanism("PLAIN");
SASLAuthentication.blacklistSASLMechanism("DIGEST-MD5");
mConnection.login("test", "ilink@2012");
} catch (XMPPException | SmackException | IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
}
@Override
public void connectionClosed() {
Log.i(TAG, "Connection closed");
}
@Override
public void connectionClosedOnError(Exception arg0) {
Log.i(TAG, "Connection closed on error");
}
@Override
public void reconnectingIn(int arg0) {
Log.i(TAG, "Reconnecting in");
}
@Override
public void reconnectionFailed(Exception arg0) {
Log.i(TAG, "Reconnection failed");
}
@Override
public void reconnectionSuccessful() {
Log.i(TAG, "Reconnection successful");
}
private class ConnectToXmppServer extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
Log.i(TAG, "Connecting to xmpp server started...");
}
@Override
protected Void doInBackground(Void... params) {
try {
XMPPTCPConnectionConfiguration config = XMPPTCPConnectionConfiguration
.builder()
.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled)
.setServiceName("192.168.0.166")
.setHost("192.168.0.166")
.setPort(5222)
.setCompressionEnabled(false).build();
mConnection = new XMPPTCPConnection(config);
mConnection.setPacketReplyTimeout(1000);
mConnection.addConnectionListener(MainActivity.this);
mConnection.connect();
} catch (XMPPException | SmackException | IOException e) {
e.printStackTrace();
Log.e(TAG, e.getMessage());
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
Log.i(TAG, "Connecting to xmpp server finished...");
}
}
}