Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here's a version working for me, using OAUTH2 tokens. The OAUTH2 scope is <code>https://www.googleapis.com/auth/googletalk</code> .</p> <p>This code uses the Smack XMPP library.</p> <p>The OAuth2 authentication mechanism :</p> <pre><code>import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.logging.Logger; import javax.security.sasl.Sasl; import org.jivesoftware.smack.SASLAuthentication; import org.jivesoftware.smack.XMPPException; import org.jivesoftware.smack.sasl.SASLMechanism; import org.jivesoftware.smack.util.Base64; public class SALSGTalkOauthMechanism extends SASLMechanism { private static final Logger log = Logger.getLogger(SALSGTalkOauthMechanism.class.getName()); public static final String NAME = "X-OAUTH2"; /** * Constructor. */ public SALSGTalkOauthMechanism(SASLAuthentication saslAuthentication) { super(saslAuthentication); log.info("Creating SASL mechanism for GTalk (X-OAUTH2)"); } @Override public void authenticate(String username, String host, String accessToken) throws IOException, XMPPException { this.hostname = host; log.info("Authenticating to host "+host+" with key "+username); String[] mechanisms = { "X-OAUTH2" }; Map&lt;String, String&gt; props = new HashMap&lt;String, String&gt;(); this.sc = Sasl.createSaslClient(mechanisms, null, "xmpp", host, props, this); getSASLAuthentication().send(new AuthMechanism(getName(), Base64.encodeBytes(('\0'+username+'\0'+accessToken).getBytes()))); } @Override protected String getName() { return NAME; } } </code></pre> <p>And the code to make it work together :</p> <pre><code>import org.jivesoftware.smack.Chat; import org.jivesoftware.smack.ChatManager; import org.jivesoftware.smack.Connection; import org.jivesoftware.smack.ConnectionConfiguration; import org.jivesoftware.smack.SASLAuthentication; import org.jivesoftware.smack.XMPPConnection; import org.jivesoftware.smack.packet.Message; import org.jivesoftware.smack.packet.Presence; import revevol.applications.cloudpulse.client.Probe; import revevol.applications.cloudpulse.client.ProbeClient; import revevol.applications.cloudpulse.client.ProbeClientUtils; import com.google.api.client.auth.oauth2.Credential; public class GTalkProbe implements Probe { private static final Logger log = Logger.getLogger(GTalkProbe.class.getName()); public static final String PROBE_GTALK_IDENTIFIER = "gtalkprobe"; @Override public void run(ProbeClient client, Properties properties) throws Exception { log.info("Start running GTalkProbe."); long startTimestamp = new Date().getTime(); Exception exception = null; MessageReboundResult result = new MessageReboundResult(); Connection conn1 = null; try { Credential credential = ProbeClientUtils.getOAuth2Credentials(properties); ConnectionConfiguration config = new ConnectionConfiguration("talk.google.com", 5222, "gmail.com"); config.setCompressionEnabled(true); config.setSASLAuthenticationEnabled(true); conn1 = new XMPPConnection(config); SASLAuthentication.registerSASLMechanism("X-OAUTH2", SALSGTalkOauthMechanism.class); SASLAuthentication.supportSASLMechanism("X-OAUTH2", 0); conn1.connect(); log.info("Logging in"); conn1.login(ProbeClientUtils.getOAuthConsumerId(properties), credential.getAccessToken()); Presence presence = new Presence(Presence.Type.available); presence.setStatus("My status"); conn1.sendPacket(presence); ChatManager chatmanager = conn1.getChatManager(); String destination = "destination@gmail.com"; log.info("Sending chat message to " + destination); String message = "PING : " + UUID.randomUUID().toString(); Chat chat = chatmanager.createChat(destination, new MessageListener(//TODO : here put your stuff)); Message msg = new Message(destination, Message.Type.chat); msg.setBody(message); chat.sendMessage(msg); //Here you are } } </code></pre>
 

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