title: Problem in Callback in Twitter in Android
body: <p>I have implemented Twitter in my application , i am facing problem in Callback.</p>
  
  <p>Twitter API's has been updated recently so i am unable to send Callback URL.</p>
  
  <p>Also the Setting Page now change there is No Option for Selecting Web Based application or Desktop Application. </p>
  
  <p>If i send Callback in this Line :</p>
  
  <pre><code>authUrl = provider.retrieveRequestToken(consumer,CALLBACK_URL);
  </code></pre>
  
  <p>It always returns </p>
  
  <pre><code>oauth.signpost.exception.OAuthNotAuthorizedException: Authorization failed (server replied with a 401). This can happen if the consumer key was not correct or the signatures did not match.
  </code></pre>
  
  <p>but if i send as <strong>Null</strong> in this its <strong>Redirecting to Twitter Login Page</strong>,but after successful Authorization it doesn't returns back to my Application.</p>
  
  <p>After the Pin number is displayed i want to Redirect back to my Application.</p>
  
  <p>Note : Twitter had updated their API so old Codes available in the Post is not working.</p>
  
  <p>I tried all the Following Links </p>
  
  <p><a href="http://code.google.com/p/oauth-signpost/" rel="nofollow">Link 1</a>,
  <a href="http://automateddeveloper.blogspot.com/2011/06/android-twitter-oauth-authentication.html" rel="nofollow">Link 2</a>,
  <a href="https://github.com/kaeppler/signpost-examples/blob/master/OAuthTwitterExample/src/TwitterMain.java" rel="nofollow">Link 3</a>,
  <a href="http://thetechnib.blogspot.com/" rel="nofollow">Link4 </a>,
  <a href="https://github.com/kaeppler/signpost" rel="nofollow">Link5 </a>,
  <a href="http://www.mobisoftinfotech.com/blog/category/android/" rel="nofollow">Link 6</a></p>
  
  <p>My Code is as follows :</p>
  
  <pre><code>public class Main extends Activity {
  OAuthConsumer consumer;
  OAuthProvider provider;
  Twitter twitter;
  private static  String CALLBACK_URL = "twitterapptest://connect";
  
  
  @Override
  public void onCreate(Bundle icicle) {
      super.onCreate(icicle);
      setContentView(R.layout.main);
  
      consumer = new DefaultOAuthConsumer(
              "XXXXXXXXXXX",
              "XXXXXXXXXXXXX");
  
      provider = new DefaultOAuthProvider(
              "https://api.twitter.com/oauth/request_token",
              "https://api.twitter.com/oauth/access_token",
              "https://api.twitter.com/oauth/authorize");
  
      String authUrl = null;
      try {
          authUrl = provider.retrieveRequestToken(consumer,null);
           this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)));
      } catch (OAuthMessageSignerException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (OAuthNotAuthorizedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (OAuthExpectationFailedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (OAuthCommunicationException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      String pin = null;
      try {
          pin = br.readLine();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      try {
          provider.retrieveAccessToken(consumer, "4947222");
      } catch (OAuthMessageSignerException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (OAuthNotAuthorizedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (OAuthExpectationFailedException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      } catch (OAuthCommunicationException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      URL url = null;
      try {
          url = new URL("http://twitter.com/statuses/mentions.xml");
      } catch (MalformedURLException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
      HttpURLConnection request = null;
      try {
          request = (HttpURLConnection) url.openConnection();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      try {
          consumer.sign(request);
      } catch (OAuthMessageSignerException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      } catch (OAuthExpectationFailedException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      } catch (OAuthCommunicationException e1) {
          // TODO Auto-generated catch block
          e1.printStackTrace();
      }
      try {
          request.connect();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  
      try {
          System.out.println("Response: " + request.getResponseCode() + " "
                  + request.getResponseMessage());
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
  }
  
  
  /**
   * 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 &amp;&amp; uri.toString().startsWith(CALLBACK_URL)) {
  
              String verifier = uri
                      .getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER);
  
              try {
                  // this will populate token and token_secret in consumer
  
                  provider.retrieveAccessToken(consumer,
                          verifier);
  
                  // TODO: you might want to store token and token_secret in you
                  // app settings!!!!!!!!
  
                  AccessToken a = new AccessToken(consumer.getToken(),
                          consumer.getTokenSecret());
  
                  // initialize Twitter4J
  
                  twitter = new TwitterFactory().getInstance();
                  twitter.setOAuthConsumer("XXXXXXX", "XXXXXXXXXX");
                  twitter.setOAuthAccessToken(a);
  
                  // create a tweet
  
                  Date d = new Date(System.currentTimeMillis());
                  String tweet = "#OAuth working! " + d.toLocaleString();
  
                  // send the tweet
  
                  twitter.updateStatus(tweet);
  
              } catch (Exception e) {
  
                  Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
              }
          }
      }
  }
  </code></pre>
  
  <p>My Manifest :</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  </code></pre>
  
  <p>
      
          
              
                  
                  
              
          </p>
  
  <pre><code>    &lt;activity android:name=".OAuthForTwitter"  android:label="@string/app_name"
              android:configChanges="orientation|keyboardHidden"  android:launchMode="singleInstance"&gt;
       &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.VIEW" /&gt;
          &lt;category android:name="android.intent.category.DEFAULT" /&gt;
          &lt;category android:name="android.intent.category.BROWSABLE" /&gt;
          &lt;data android:scheme="twitterapptest" android:host="connect" /&gt;
      &lt;/intent-filter&gt;
  
  &lt;/activity&gt;
  
  &lt;/application&gt;
  &lt;uses-sdk android:minSdkVersion="4" /&gt;
  &lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  </code></pre>
  
  <p> </p>