2013-07-09 98 views
2

我試圖使用此代碼來獲取我的android應用程序中的twitter訪問令牌。 「provider.retrieveRequestToken(consumer,CALLBACK_URL)」方法會導致異常,並且日誌爲「與服務提供者的通信失敗:null」。與服務提供商的通信失敗:null推特&android

下面的代碼是我使用的類:

public class MainActivity extends Activity { 

    private Twitter twitter; 
    private static final String CONSUMER_KEY = "***"; 
    private static final String CONSUMER_SECRET = "*******";  

    private static final String REQUEST_URL = "https://api.twitter.com/oauth/request_token"; 
    private static final String ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token"; 
    private static final String AUTH_URL = "https://api.twitter.com/oauth/authorize"; 
    private static final String CALLBACK_URL = "https://twitter.com"; 

    private static CommonsHttpOAuthConsumer consumer; 
    private static CommonsHttpOAuthProvider provider; 
    //private static OAuthProvider provider; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Toast.makeText(getBaseContext(), "onCreate", Toast.LENGTH_LONG).show(); 
    Button btn = (Button) findViewById(R.id.btn_tweet); 

    btn.setOnClickListener(new View.OnClickListener() { 


    public void onClick(View v) { 
     askOAuth(); 

     } 
    }); 
    } 
    private void askOAuth() { 
     try { 
      consumer = new CommonsHttpOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
      provider = new CommonsHttpOAuthProvider(REQUEST_URL, ACCESS_TOKEN_URL, AUTH_URL);   

      String authUrl = provider.retrieveRequestToken(consumer, CALLBACK_URL); 
      Toast.makeText(this, authUrl, Toast.LENGTH_LONG).show(); 

      this.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); 
     } catch (Exception e) { 
      Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
     } 
    } 

    @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.signpost.OAuth.OAUTH_VERIFIER); 

      try { 
       provider.retrieveAccessToken(consumer, verifier); 
       AccessToken a = new AccessToken(consumer.getToken(), consumer.getTokenSecret()); 
       twitter = new TwitterFactory().getInstance(); 
       twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET); 
       twitter.setOAuthAccessToken(a); 
      } catch (Exception e) { 
       Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 

     } 
    } 
} 

這是我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.panpan" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="17" /> 

    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permisssion.ACCESS_FINE_LOCATION" /> 
    <uses-permission android:name="android.permisssion.ACCESS_COARSE_LOCATION" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.panpan.MainActivity" 
      android:label="@string/title_activity_login" 
      android:windowSoftInputMode="adjustResize|stateVisible" 
      android:launchMode="singleInstance"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     <intent-filter> 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" /> 
      <category android:name="android.intent.category.BROWSABLE" /> 
      <data android:scheme="https" android:host="twitter.com" /> 
     </intent-filter> 

     </activity> 

    </application> 

</manifest> 

回答

0

做provider.retrieveRequestToken在一個單獨的AsyncTask。這應該工作。

0

我有同樣的問題(這隻發生在Android 4.X)。你可以使用StrictMode.enableDefaults();或者你可以使用下一個AsyncTask(更好,因爲它不會阻塞UI線程)。

 mAuthTask = new RequestTask(); 
    mAuthTask.execute((Void) null);// Execute background task 
    /** 
* Represents an asynchronous login/registration task used to authenticate 
* the user. 
*/ 
public class RequestTask extends AsyncTask<Void, Void, Boolean> { 
    String content; 

    protected void onPreExecute() { 
    } 

    protected Boolean doInBackground(Void... params) { 
     httpOauthConsumer = new CommonsHttpOAuthConsumer(
       TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET); 
     httpOauthprovider = new DefaultOAuthProvider(REQUEST_URL, 
       ACCESS_URL, AUTHORIZE_URL); 

     try { 
      authUrl = httpOauthprovider.retrieveRequestToken(
        httpOauthConsumer, CALLBACK_URL); 
     } catch (OAuthMessageSignerException e) { 
      e.printStackTrace(); 
      return false; 
     } catch (OAuthNotAuthorizedException e) { 
      e.printStackTrace(); 
      return false; 
     } catch (OAuthExpectationFailedException e) { 
      e.printStackTrace(); 
      return false; 
     } catch (OAuthCommunicationException e) { 
      e.printStackTrace(); 
      return false; 
     } 
     return true; 
    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 
     if (success == true) { 
      Log.e("LoginUsingTwitterActivity", "onPostExecute"); 
      startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl))); 
     } 
    } 

    @Override 
    protected void onCancelled() { 
     mAuthTask = null; 
    } 
} 
相關問題