2014-01-22 116 views
0

我想Twitter的登錄我的Android應用程序從這個網址整合 http://androidcodeexamples.blogspot.in/2011/12/how-to-integrate-twitter-in-android.htmlTwitter與android oAuth問題集成?

似乎登錄它,但不知何故不能生成訪問令牌 什麼我相信到現在是,也許Twitter已經修改了其訪問令牌網址......

private static final String TWITTER_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token"; 
private static final String TWITTER_AUTHORZE_URL = "https://api.twitter.com/oauth/authorize"; 
private static final String TWITTER_REQUEST_URL = "https://api.twitter.com/oauth/request_token"; 

因此,誰能幫什麼網址爲訪問令牌

回答

0

設置試一下

private Handler mHandler = new Handler() { 
    @Override 
    public void handleMessage(Message msg) { 
     switch(msg.what){ 
      case HIDE_PROGRESS_DIALOG:{ 
       hideProgressDialog(); 
      }break; 
      case PROCESS_TOKEN:{ 
       processToken(url); 
      }break; 
     } 
    } 
} 

@Override 
public void create() { 
    LinearLayout mainLayout = new LinearLayout(this); 
    mainLayout.setOrientation(LinearLayout.VERTICAL); 
    mainLayout.setBackgroundColor(Color.parseColor("#ffffff")); 
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
      LinearLayout.LayoutParams.FILL_PARENT, 
      LinearLayout.LayoutParams.FILL_PARENT); 
    mainLayout.setLayoutParams(lp); 

    webView = new WebView(this); 
    webView.setLayoutParams(lp); 

    mainLayout.addView(webView); 

    setContentView(mainLayout); 

    webView.getSettings().setJavaScriptEnabled(true); 
    webView.setOnTouchListener(new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getAction()) { 
       case MotionEvent.ACTION_DOWN:{ 
       }break;  
       case MotionEvent.ACTION_UP:{ 
        if (!v.hasFocus()) { 
         v.requestFocus(); 
        } 
       }break; 
      } 
      return false; 
     } 
    }); 
    webView.setWebViewClient(new WebViewClient(){ 

     @Override 
      public void onPageStarted(WebView view, String url, Bitmap favicon) { 
       super.onPageStarted(view, url, favicon); 
      } 

     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

     if (url.startsWith(CALLBACK_URL)) { 
       TwitterAuthorizationActivity.this.url = url; 

       mHandler.sendEmptyMessage(PROCESS_TOKEN); 

       return true; 
     }else if (url.startsWith("authorize")) { 
       return false; 
     } 

      return true; 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
     } 



    }); 

    mTwitter = new TwitterFactory().getInstance(); 

    mHttpOauthConsumer = new CommonsHttpOAuthConsumer(twitterConsumerKey, twitterSecretKey); 
    mHttpOauthprovider = new CommonsHttpOAuthProvider("https://twitter.com/oauth/request_token", 
      "https://twitter.com/oauth/access_token", 
      "https://twitter.com/oauth/authorize"); 

    try{ 
     authUrl = mHttpOauthprovider.retrieveRequestToken(mHttpOauthConsumer, CALLBACK_URL); 
    }catch(OAuthCommunicationException oACEx){ 
     Log.d("", ""); 
    }catch(OAuthMessageSignerException oAMSEx){ 
     Log.d("", ""); 
    }catch(OAuthNotAuthorizedException oANAEx){ 
     Log.d("", ""); 
    }catch(OAuthExpectationFailedException oAEFEx){ 
     Log.d("", ""); 
    } 

    webView.loadUrl(authUrl); 
} 

public void processToken(String callbackUrl) { 
    final String verifier = getVerifier(callbackUrl); 

    int what = 1; 

    try { 
     mHttpOauthprovider.retrieveAccessToken(mHttpOauthConsumer, verifier); 

     mAccessToken = new AccessToken(mHttpOauthConsumer.getToken(), mHttpOauthConsumer.getTokenSecret()); 

     mTwitter.setOAuthConsumer(twitterConsumerKey, twitterSecretKey); 

     mTwitter.setOAuthAccessToken(mAccessToken); 

     what = 0; 
    } catch (Exception e){ 
    } 
} 

private String getVerifier(String callbackUrl) { 
    String verifier = ""; 

    try { 
     callbackUrl = callbackUrl.replace("twitterapp", "http"); 

     URL url = new URL(callbackUrl); 
     String query = url.getQuery(); 

     String array[] = query.split("&"); 

     for (String parameter : array) { 
      String v[] = parameter.split("="); 

      if (URLDecoder.decode(v[0]).equals(oauth.signpost.OAuth.OAUTH_VERIFIER)) { 
       verifier = URLDecoder.decode(v[1]); 
       break; 
      } 
     } 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } 

    return verifier; 
} 

此解決方案使用Twitter4J庫。