You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@esme.apache.org by Chatree Srichart <ch...@gmail.com> on 2010/01/14 07:28:02 UTC

How to update a ESME status with a Java application?

Hi, all

I try to update a ESME status with a Java application.

=== Java Code ===

String twitterUrl = "http://localhost:8080/twitter/statuses/update.xml";
String username = "myUserName@email.com";
String password = "myPassword";

OutputStreamWriter writer = null;
String statusMessage = "Test Status";

 try {
        URL url = new URL(twitterUrl);
        HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
        connection.setDoOutput(true);
        String authorization = username + ":" + password;
        String encoding = Base64.base64Encode(authorization);
        connection.setRequestProperty("Authorization", "Basic " +
encoding.getBytes());

        String encStatus = "status=" + URLEncoder.encode(statusMessage,
"UTF-8");
        writer = new OutputStreamWriter(connection.getOutputStream());
        writer.write(encStatus);
        writer.flush();

        int responseCode = connection.getResponseCode();
        System.out.println("responseCode:" + responseCode);
    } catch (Exception e) {
        e.printStackTrace();
    }

==============
But I get 401 as response code. It look like authentication fail.
Can any one tell me what I was wrong and where is ESME document I have to
look for updating status form a Java application?

Re: How to update a ESME status with a Java application?

Posted by Chatree Srichart <ch...@gmail.com>.
I'm sorry. I just miss ".[protocol]". The URL can be like this:

http://[host][url prefix][method].[protocol]

Re: How to update a ESME status with a Java application?

Posted by Richard Hirsch <hi...@gmail.com>.
Regarding the URL to use the twitter api, you have multiple options
depending on the implementation.

The java code you initially posted looks correct (you can compare it
to my code here :
http://cwiki.apache.org/ESME/simple-use-case-ofbiz-sends-message-to-esme.html)
. In such Java implementations, the URL would be
"http://localhost:8081/twitter/statuses/update.xml". In such
situations, you do not include the basic authentication in the URL but
add it via the Request Parameter "Authorization",

The use of basic authentication via URL
("http://[username]:[token]@[host]/[url prefix]/[command]") is usually
just for actions (http://cwiki.apache.org/ESME/actions.html) -
especially those including http posts and other types of
implementations.

D.

On Thu, Jan 14, 2010 at 9:34 AM, Chatree Srichart
<ch...@gmail.com> wrote:
> Thank you very much. It's work fine.
> I don't know before what URL pattern should be like this: http://
> [username]:[token]@[
> host]/[url prefix].[protocol]
> Can you tell me where ESME document to describe this subject?
>
> Regards,
>

Re: How to update a ESME status with a Java application?

Posted by Chatree Srichart <ch...@gmail.com>.
Thank you very much. It's work fine.
I don't know before what URL pattern should be like this: http://
[username]:[token]@[
host]/[url prefix].[protocol]
Can you tell me where ESME document to describe this subject?

Regards,

Re: How to update a ESME status with a Java application?

Posted by Richard Hirsch <hi...@gmail.com>.
I just sent you (in a private email) a test user + token for our test
instance on the cloud, could you try it there...

D.

On Thu, Jan 14, 2010 at 8:41 AM, Chatree Srichart
<ch...@gmail.com> wrote:
> The Log is "INFO - Service request (POST) /twitter/statuses/update/ took 46
> Milliseconds".
> When I enter the URL to a web browser, I am prompted a message "The
> requested page was not defined in your SiteMap, so access was blocked. (This
> message is displayed in development mode only)"
>
> Regards,
>

Re: How to update a ESME status with a Java application?

Posted by Chatree Srichart <ch...@gmail.com>.
The Log is "INFO - Service request (POST) /twitter/statuses/update/ took 46
Milliseconds".
When I enter the URL to a web browser, I am prompted a message "The
requested page was not defined in your SiteMap, so access was blocked. (This
message is displayed in development mode only)"

Regards,

Re: How to update a ESME status with a Java application?

Posted by Richard Hirsch <hi...@gmail.com>.
What is in the ESME Log?

On Thu, Jan 14, 2010 at 8:22 AM, Chatree Srichart
<ch...@gmail.com> wrote:
> Hi, Richard Hirsch
>
> Thank you very much. It really worked. But I get other error, I get 403 as
> response code.
>

Re: How to update a ESME status with a Java application?

Posted by Chatree Srichart <ch...@gmail.com>.
Hi, Richard Hirsch

Thank you very much. It really worked. But I get other error, I get 403 as
response code.

Re: How to update a ESME status with a Java application?

Posted by Richard Hirsch <hi...@gmail.com>.
One more thing: the username isn't the user's email but rather the user-id

On Thu, Jan 14, 2010 at 7:54 AM, Richard Hirsch <hi...@gmail.com> wrote:
> You need to use the token instead of the user's password.
>
> Here is the help page from the Web Ui with details on how to do it:
> http://cwiki.apache.org/ESME/web-ui-help.html -- just search for the
> word "Manage Tokens"
>
> D.
>
>
> On Thu, Jan 14, 2010 at 7:28 AM, Chatree Srichart
> <ch...@gmail.com> wrote:
>> Hi, all
>>
>> I try to update a ESME status with a Java application.
>>
>> === Java Code ===
>>
>> String twitterUrl = "http://localhost:8080/twitter/statuses/update.xml";
>> String username = "myUserName@email.com";
>> String password = "myPassword";
>>
>> OutputStreamWriter writer = null;
>> String statusMessage = "Test Status";
>>
>>  try {
>>        URL url = new URL(twitterUrl);
>>        HttpURLConnection connection = (HttpURLConnection)
>> url.openConnection();
>>        connection.setDoOutput(true);
>>        String authorization = username + ":" + password;
>>        String encoding = Base64.base64Encode(authorization);
>>        connection.setRequestProperty("Authorization", "Basic " +
>> encoding.getBytes());
>>
>>        String encStatus = "status=" + URLEncoder.encode(statusMessage,
>> "UTF-8");
>>        writer = new OutputStreamWriter(connection.getOutputStream());
>>        writer.write(encStatus);
>>        writer.flush();
>>
>>        int responseCode = connection.getResponseCode();
>>        System.out.println("responseCode:" + responseCode);
>>    } catch (Exception e) {
>>        e.printStackTrace();
>>    }
>>
>> ==============
>> But I get 401 as response code. It look like authentication fail.
>> Can any one tell me what I was wrong and where is ESME document I have to
>> look for updating status form a Java application?
>>
>

Re: How to update a ESME status with a Java application?

Posted by Richard Hirsch <hi...@gmail.com>.
You need to use the token instead of the user's password.

Here is the help page from the Web Ui with details on how to do it:
http://cwiki.apache.org/ESME/web-ui-help.html -- just search for the
word "Manage Tokens"

D.


On Thu, Jan 14, 2010 at 7:28 AM, Chatree Srichart
<ch...@gmail.com> wrote:
> Hi, all
>
> I try to update a ESME status with a Java application.
>
> === Java Code ===
>
> String twitterUrl = "http://localhost:8080/twitter/statuses/update.xml";
> String username = "myUserName@email.com";
> String password = "myPassword";
>
> OutputStreamWriter writer = null;
> String statusMessage = "Test Status";
>
>  try {
>        URL url = new URL(twitterUrl);
>        HttpURLConnection connection = (HttpURLConnection)
> url.openConnection();
>        connection.setDoOutput(true);
>        String authorization = username + ":" + password;
>        String encoding = Base64.base64Encode(authorization);
>        connection.setRequestProperty("Authorization", "Basic " +
> encoding.getBytes());
>
>        String encStatus = "status=" + URLEncoder.encode(statusMessage,
> "UTF-8");
>        writer = new OutputStreamWriter(connection.getOutputStream());
>        writer.write(encStatus);
>        writer.flush();
>
>        int responseCode = connection.getResponseCode();
>        System.out.println("responseCode:" + responseCode);
>    } catch (Exception e) {
>        e.printStackTrace();
>    }
>
> ==============
> But I get 401 as response code. It look like authentication fail.
> Can any one tell me what I was wrong and where is ESME document I have to
> look for updating status form a Java application?
>