title: Twitter - retrieve pin code from webview. Android
body: <p>Is it possible to somehow retrieve the pin code from WebView in Android while authorising the Twitter? </p>
  
  <p>I've got tutorial example, but to login user need to rewrite the pin code from WebView to app. I just want to pass this and get pin code directly from web.</p>
  
  <p>This is very similar to how it goes on iPhone. </p>
  
  <p>Now I using something like this:</p>
  
  <pre><code>public void getTwitter() 
  {
        handleEvent.post(new Runnable() 
        {
  
         @Override
         public void run() 
         {               
              if (oHelper.hasAccessToken()) 
              {
  
                  oHelper.configureOAuth(twitterConnection);
                  try 
                  {
                       twitterConnection.updateStatus("Hii!!!!");
                  } 
                  catch (TwitterException e) 
                  {
                       Log.d("TWEET", "Error Updating status " + e.getMessage());
                       e.printStackTrace();
                  }
              } 
              else 
              {
                   try 
                   {
                        twitterConnection.setOAuthConsumer(CONSUMER_KEY,CONSUMER_SECRET);
                        requestToken = twitterConnection.getOAuthRequestToken("");
                        webViewDialog(requestToken.getAuthorizationURL(), 0);
                   } 
                   catch (TwitterException e) 
                   {
                       e.printStackTrace();
                   }
              }
         }
  
        });
  }
  </code></pre>
  
  <p>then display the webview inside dialog popup:</p>
  
  <pre><code>private void webViewDialog(final String authorizationURL, final int type) 
  {
        LinearLayout container = new LinearLayout(this);
        container.setMinimumWidth(200);
        container.setMinimumHeight(320);
        webView = new WebView(this);
  
        webView.setMinimumWidth(200);
        webView.setMinimumHeight(380);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient());
        webView.loadUrl(authorizationURL);
  
        container.addView(webView);
        webDialog = new AlertDialog.Builder(this);
        webDialog.setView(container).setPositiveButton("Ok", new DialogInterface.OnClickListener() 
        {
  
           @Override
           public void onClick(DialogInterface dialog, int which) 
           {
               dia = dialog;
                if (type == 0) 
                {
                     twitterPinCodeDialog();
                }
  
           }
        }).show();
  }
  </code></pre>
  
  <p>and in the end prompt user to input pincode:</p>
  
  <pre><code>public void twitterPinCodeDialog() 
  {
        LinearLayout pinHolder = new LinearLayout(this);
        pinHolder.setGravity(Gravity.CENTER);
        final EditText editPinCode = new EditText(this);
        editPinCode.setGravity(Gravity.CENTER);
        editPinCode.setHint("Pin Code");
        editPinCode.setWidth(200);
        pinHolder.addView(editPinCode);
  
        Builder pinBuilder = new AlertDialog.Builder(this);
        pinBuilder.setView(pinHolder).setTitle("Twitter Pin Code Entry")
          .setMessage("Please enter displayed twitter code into the field")
          .setPositiveButton("Ok", new DialogInterface.OnClickListener() 
          {
  
           @Override
           public void onClick(DialogInterface dialog, int which) 
           {
                if (editPinCode.getText().toString() != null &amp;&amp; !editPinCode.getText().toString().equals("")) 
                {
                     try 
                     {
                          accessToken = twitterConnection.getOAuthAccessToken(requestToken,editPinCode.getText().toString());
                          oHelper.storeAccessToken(accessToken);
                          Log.i("Access Token:", accessToken.getToken());
                          Log.i("Access Secret:", accessToken.getTokenSecret());
                          twitterConnection.updateStatus("Tweeted Successfully");
                     } 
                     catch (TwitterException te) 
                     {
                          if (401 == te.getStatusCode()) 
                          {
                              System.out.println("Unable to get the access token.");
                          } 
                          else 
                          {
                              te.printStackTrace();
                          }
                     }
                } 
                else 
                {
                 Toast.makeText(context, "Pin code is required",
                   Toast.LENGTH_SHORT).show();
                 dialog.dismiss();    
                 dia.dismiss();
                 twitterPinCodeDialog();
                 }
           }
  
          }).setNegativeButton("Cancel",new DialogInterface.OnClickListener() 
          {
  
             @Override
             public void onClick(DialogInterface dialog,int which) 
             {
                  Toast.makeText(context,"To share your news please complete login next time",Toast.LENGTH_LONG).show();
                  dialog.dismiss();
             }
            }).show();
       }
  </code></pre>
  
  <p>I don't even remember from where I took this example. 
  But it would be nice to get rid off last dialog.</p>