Re: [twitter-dev] Problems with Twitter4J and OAuth on Android

2010-07-21 Thread Taylor Singletary
Hi Clay,

Just noticed that you have an API secret in this code sample you've
provided -- you'll want to go to your application record and
regenerate a new key/secret pair.

As for the code itself -- I see you setting your consumer key and
secret as constants near the top, but then in askOAuth you're using
different values -- is this just a result of a bad paste?

Taylor

On Tue, Jul 20, 2010 at 11:18 PM, Clay Graham  wrote:
> Cant figure out if this is a problem with Twitter4J or the Twitter
> API. I am trying to create an OAuth callback activity, started by a
> RequestToken, but the Request Token request is failing. DOES ANYONE
> KNOW IF GOOGLE HAS MADE THE TWITTER CLIENT AVAILABLE BY OPEN SOURCE
> YET?
>
> Twitter4J 2.1.2
> Android Froyo 2.2
>
> package com.sightlyinc.oauth.android;
>
> import java.util.Date;
>
> import twitter4j.Twitter;
> import twitter4j.TwitterFactory;
> import twitter4j.http.AccessToken;
> import twitter4j.http.RequestToken;
> import android.app.Activity;
> import android.content.Intent;
> import android.content.SharedPreferences;
> import android.content.SharedPreferences.Editor;
> import android.net.Uri;
> import android.os.Bundle;
> import android.preference.PreferenceManager;
> import android.util.Log;
> import android.view.View;
> import android.view.View.OnClickListener;
> import android.widget.Button;
> import android.widget.EditText;
> import android.widget.Toast;
>
> public class SightlyOauthActivity extends Activity {
>
>    private static final String TAG =
> SightlyOauthActivity.class.getName();
>
>    private Twitter twitter;
>    private RequestToken requestToken;
>    private SharedPreferences mSharedPreferences;
>
>    private String CONSUMER_KEY =           "yfKRsmTgi8UT8eHoV5Khrw";
>    private String CALLBACK_URL =           "sightlyoauth://oauth";
>
>    private EditText tweetTextView;
>    private Button buttonLogin;
>
>        /** Called when the activity is first created. */
>       �...@override
>        public void onCreate(Bundle savedInstanceState) {
>                super.onCreate(savedInstanceState);
>                setContentView(R.layout.main);
>
>                mSharedPreferences =
>                        PreferenceManager.getDefaultSharedPreferences(
>                                        getApplicationContext());
>
>                tweetTextView =
> (EditText)findViewById(R.id.TweetView);
>        buttonLogin = (Button)findViewById(R.id.ButtonLogon);
>        buttonLogin.setOnClickListener(new OnClickListener() {
>                public void onClick(View v) {
>                        askOAuth();
>                }
>        });
>
>        }
>
>        /**
>         * Open the browser and asks the user to authorize the app.
> Afterwards, we
>         * redirect the user back here!
>         */
>        private void askOAuth() {
>                try {
>
>                        //setup properties, https was attempted and I
> was advised to
>                        //use HTTPS instead.
>
> System.setProperty("twitter4j.http.useSSL","false");
>
> System.setProperty("twitter4j.oauth.consumerKey","MYKEY");
>
> System.setProperty("twitter4j.oauth.consumerSecret","MYSECRET");
>
> System.setProperty("twitter4j.oauth.requestTokenURL","http://
> api.twitter.com/oauth/request_token");
>
> System.setProperty("twitter4j.oauth.accessTokenURL","http://
> api.twitter.com/oauth/access_token");
>
> System.setProperty("twitter4j.oauth.authorizationURL","http://
> api.twitter.com/oauth/authorize");
>
>                        //get the instance
>                        Twitter twitter =
>                                new TwitterFactory().getInstance();
>
>                        //THIS EXCEPTS WITH PERMISSION DENIED
>                        requestToken =
> twitter.getOAuthRequestToken(CALLBACK_URL);
>
>                        //set the request token info
>                        Toast.makeText(this, "Please authorize this
> app!",
>                                        Toast.LENGTH_LONG).show();
>
>                        this.startActivity(new
> Intent(Intent.ACTION_VIEW, Uri
>                                        
> .parse(requestToken.getAuthenticationURL(;
>
>                } catch (Exception e) {
>                        Log.e(TAG, e.getMessage(),e);
>                        Toast.makeText(this, e.getMessage(),
> Toast.LENGTH_LONG).show();
>                }
>        }
>
>        /**
>         * As soon as the user successfully authorized the app, we are
> notified
>         * here. Now we need to get the verifier from the callback
> URL,
> retrieve
>         * token and token_secret and feed them to twitter4j (as well
> as
> consumer
>         * key and secret).
>         */
>       �...@override
>        protected void onNewIntent(Intent intent) {
>
>                super.onNewIntent(intent);
>
>                Uri uri = intent.getData();
>                if (uri != null &&
> uri.toString().startsWith(CALLBACK_URL)) {
>
>                        St

[twitter-dev] Problems with Twitter4J and OAuth on Android

2010-07-21 Thread Clay Graham
Cant figure out if this is a problem with Twitter4J or the Twitter
API. I am trying to create an OAuth callback activity, started by a
RequestToken, but the Request Token request is failing. DOES ANYONE
KNOW IF GOOGLE HAS MADE THE TWITTER CLIENT AVAILABLE BY OPEN SOURCE
YET?

Twitter4J 2.1.2
Android Froyo 2.2

package com.sightlyinc.oauth.android;

import java.util.Date;

import twitter4j.Twitter;
import twitter4j.TwitterFactory;
import twitter4j.http.AccessToken;
import twitter4j.http.RequestToken;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SightlyOauthActivity extends Activity {

private static final String TAG =
SightlyOauthActivity.class.getName();

private Twitter twitter;
private RequestToken requestToken;
private SharedPreferences mSharedPreferences;

private String CONSUMER_KEY =   "yfKRsmTgi8UT8eHoV5Khrw";
private String CONSUMER_SECRET =
"4MIf93Wv35Choa5zXM0gQqIUD8uUFNcvAq1TPuBc";
private String CALLBACK_URL =   "sightlyoauth://oauth";

private EditText tweetTextView;
private Button buttonLogin;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mSharedPreferences =
PreferenceManager.getDefaultSharedPreferences(
getApplicationContext());

tweetTextView =
(EditText)findViewById(R.id.TweetView);
buttonLogin = (Button)findViewById(R.id.ButtonLogon);
buttonLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
askOAuth();
}
});

}

/**
 * Open the browser and asks the user to authorize the app.
Afterwards, we
 * redirect the user back here!
 */
private void askOAuth() {
try {

//setup properties, https was attempted and I
was advised to
//use HTTPS instead.
 
System.setProperty("twitter4j.http.useSSL","false");
 
System.setProperty("twitter4j.oauth.consumerKey","MYKEY");
 
System.setProperty("twitter4j.oauth.consumerSecret","MYSECRET");
 
System.setProperty("twitter4j.oauth.requestTokenURL","http://
api.twitter.com/oauth/request_token");
 
System.setProperty("twitter4j.oauth.accessTokenURL","http://
api.twitter.com/oauth/access_token");
 
System.setProperty("twitter4j.oauth.authorizationURL","http://
api.twitter.com/oauth/authorize");

//get the instance
Twitter twitter =
new TwitterFactory().getInstance();

//THIS EXCEPTS WITH PERMISSION DENIED
requestToken =
twitter.getOAuthRequestToken(CALLBACK_URL);

//set the request token info
Toast.makeText(this, "Please authorize this
app!",
Toast.LENGTH_LONG).show();

this.startActivity(new
Intent(Intent.ACTION_VIEW, Uri

.parse(requestToken.getAuthenticationURL(;

} catch (Exception e) {
Log.e(TAG, e.getMessage(),e);
Toast.makeText(this, e.getMessage(),
Toast.LENGTH_LONG).show();
}
}

/**
 * As soon as the user successfully authorized the app, we are
notified
 * here. Now we need to get the verifier from the callback
URL,
retrieve
 * token and token_secret and feed them to twitter4j (as well
as
consumer
 * key and secret).
 */
@Override
protected void onNewIntent(Intent intent) {

super.onNewIntent(intent);

Uri uri = intent.getData();
if (uri != null &&
uri.toString().startsWith(CALLBACK_URL)) {

String verifier = uri
.getQueryParameter("oauth_verifier");

try {

//this user has verified
AccessToken accessToken =
 
twitter.getOAuthAccessToken(requestToken, verifier);

//save the access token
Editor e = mSharedPreferences.edit();
e.putString("AccessToken.token",
accessToken.getToken());