2012-04-20 121 views
0

我正在使用Twitter oAuth系统来开发我的应用程序,具体介绍了本教程:http://www.markhneedham.com/blog/2012/01/02/learning-android-authenticating-with-twitter-using-oauth/。我似乎根据我的回调网址获得了一个错误,而且我无法在任何地方找到正确的解决方案或适当的信息。我的应用程序的名字是chirpee,虐待代码和错误,任何帮助将不胜感激!oAuth CALLBACKURL for android/twitter dev的问题(版本2.3.3)

注意:当我尝试通过twitter开发者将它们设置在我的应用程序页面上时,许多教程都有像“app:// twitter”这样的CALLBACKURL的随机Twitter网址,它们是“无效网址”。目前我的CALLBACKURL设置为http://crownseal.ca(随机站点)在我的应用程序的开发者页面和代码中。

package me.chirpee.tom; 

import oauth.signpost.OAuthProvider; 
import oauth.signpost.basic.DefaultOAuthProvider; 
import oauth.signpost.commonshttp.CommonsHttpOAuthConsumer; 
import android.app.Activity; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.SyncStateContract.Constants; 
import android.util.Log; 
import android.view.View; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.Toast; 

public class Chirpee extends Activity 
{ 
    //variables 
    private String CALLBACKURL = "http://crownseal.ca"; 
    private String consumerKey = "----"; 
    private String consumerSecret = "----"; 
    private OAuthProvider httpOauthprovider = new DefaultOAuthProvider("https://api.twitter.com/oauth/request_token", "https://api.twitter.com/oauth/access_token", "https://api.twitter.com/oauth/authorize"); 
    private CommonsHttpOAuthConsumer httpOauthConsumer = new CommonsHttpOAuthConsumer(consumerKey, consumerSecret); 


    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     Button oauth = (Button) findViewById(R.id.button1); 
     oauth.setOnClickListener(new View.OnClickListener() 
     { 
     public void onClick(View v) 
     { 
      try 
      { 
      String authUrl = httpOauthprovider.retrieveRequestToken(httpOauthConsumer, CALLBACKURL); 
      Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(authUrl)); 
      v.getContext().startActivity(intent); 
      } 
      catch (Exception e) 
      { 
      Log.w("oauth fail", e); 
      Toast.makeText(v.getContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 
      } 
     } 
     });//end onClickListener() 

    }//end onCreate() 




    @Override 
    protected void onNewIntent(Intent intent) { 
     super.onNewIntent(intent); 

     Log.w("redirect-to-app", "going to save the key and secret"); 

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

      String verifier = uri.getQueryParameter(oauth.signpost.OAuth.OAUTH_VERIFIER); 

      try { 
       // this will populate token and token_secret in consumer 

       httpOauthprovider.retrieveAccessToken(httpOauthConsumer, verifier); 
       String userKey = httpOauthConsumer.getToken(); 
       String userSecret = httpOauthConsumer.getTokenSecret(); 

       // Save user_key and user_secret in user preferences and return 
       SharedPreferences settings = getBaseContext().getSharedPreferences("your_app_prefs", 0); 
       SharedPreferences.Editor editor = settings.edit(); 
       editor.putString("user_key", userKey); 
       editor.putString("user_secret", userSecret); 
       editor.commit(); 

      } catch (Exception e) { 

      } 
     } else { 
      // Do something if the callback comes from elsewhere 
     } 
    } 

}

,并在我的清单文件,我有以下几点:

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

<uses-sdk android:minSdkVersion="10" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" > 
    <activity 
     android:label="@string/app_name" 
     android:name=".Chirpee" 
     android:launchMode="singleInstance"> 

     <intent-filter > 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 


     <!-- Used for OAuth callback --> 
     <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="http" android:host="crownseal.ca" /> 
     </intent-filter> 

    </activity> 
</application> 

</manifest> 

和是我的错误如下:

04-20 18:01:26.017: D/AndroidRuntime(1128): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< 
04-20 18:01:26.017: D/AndroidRuntime(1128): CheckJNI is ON 
04-20 18:01:26.726: D/AndroidRuntime(1128): Calling main entry com.android.commands.pm.Pm 
04-20 18:01:26.766: D/AndroidRuntime(1128): Shutting down VM 
04-20 18:01:26.786: D/dalvikvm(1128): GC_CONCURRENT freed 101K, 71% free 297K/1024K, external 0K/0K, paused 2ms+1ms 
04-20 18:01:26.786: D/dalvikvm(1128): Debugger has detached; object registry had 1 entries 
04-20 18:01:26.816: I/AndroidRuntime(1128): NOTE: attach of thread 'Binder Thread #3' failed 
04-20 18:01:27.236: D/AndroidRuntime(1138): >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<< 
04-20 18:01:27.236: D/AndroidRuntime(1138): CheckJNI is ON 
04-20 18:01:27.897: D/AndroidRuntime(1138): Calling main entry com.android.commands.am.Am 
04-20 18:01:27.937: I/ActivityManager(60): Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=me.chirpee.tom/.Chirpee } from pid 1138 
04-20 18:01:27.947: W/redirect-to-app(1099): going to save the key and secret 
04-20 18:01:27.957: D/AndroidRuntime(1138): Shutting down VM 
04-20 18:01:27.977: D/dalvikvm(1138): GC_CONCURRENT freed 102K, 69% free 319K/1024K, external 0K/0K, paused 2ms+2ms 
04-20 18:01:27.977: D/dalvikvm(1138): Debugger has detached; object registry had 1 entries 
04-20 18:01:28.007: I/AndroidRuntime(1138): NOTE: attach of thread 'Binder Thread #3' failed 
04-20 18:01:33.676: W/oauth fail(1099): oauth.signpost.exception.OAuthCommunicationException: Communication with the service provider failed: api.twitter.com 
04-20 18:01:33.676: W/oauth fail(1099):  at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:214) 
04-20 18:01:33.676: W/oauth fail(1099):  at oauth.signpost.AbstractOAuthProvider.retrieveRequestToken(AbstractOAuthProvider.java:69) 
04-20 18:01:33.676: W/oauth fail(1099):  at me.chirpee.tom.Chirpee$1.onClick(Chirpee.java:41) 
04-20 18:01:33.676: W/oauth fail(1099):  at android.view.View.performClick(View.java:2485) 
04-20 18:01:33.676: W/oauth fail(1099):  at android.view.View$PerformClick.run(View.java:9080) 
04-20 18:01:33.676: W/oauth fail(1099):  at android.os.Handler.handleCallback(Handler.java:587) 
04-20 18:01:33.676: W/oauth fail(1099):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-20 18:01:33.676: W/oauth fail(1099):  at android.os.Looper.loop(Looper.java:123) 
04-20 18:01:33.676: W/oauth fail(1099):  at android.app.ActivityThread.main(ActivityThread.java:3683) 
04-20 18:01:33.676: W/oauth fail(1099):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-20 18:01:33.676: W/oauth fail(1099):  at java.lang.reflect.Method.invoke(Method.java:507) 
04-20 18:01:33.676: W/oauth fail(1099):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
04-20 18:01:33.676: W/oauth fail(1099):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
04-20 18:01:33.676: W/oauth fail(1099):  at dalvik.system.NativeStart.main(Native Method) 
04-20 18:01:33.676: W/oauth fail(1099): Caused by: java.net.UnknownHostException: api.twitter.com 
04-20 18:01:33.676: W/oauth fail(1099):  at java.net.InetAddress.lookupHostByName(InetAddress.java:497) 
04-20 18:01:33.676: W/oauth fail(1099):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:294) 
04-20 18:01:33.676: W/oauth fail(1099):  at java.net.InetAddress.getAllByName(InetAddress.java:256) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:69) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.<init>(HttpConnection.java:48) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection$Address.connect(HttpConnection.java:322) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnectionPool.get(HttpConnectionPool.java:89) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getHttpConnection(HttpURLConnectionImpl.java:285) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.makeConnection(HttpURLConnectionImpl.java:267) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:406) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl$HttpsEngine.makeConnection(HttpsURLConnectionImpl.java:387) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:205) 
04-20 18:01:33.676: W/oauth fail(1099):  at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:152) 
04-20 18:01:33.676: W/oauth fail(1099):  at oauth.signpost.basic.DefaultOAuthProvider.sendRequest(DefaultOAuthProvider.java:48) 
04-20 18:01:33.676: W/oauth fail(1099):  at oauth.signpost.AbstractOAuthProvider.retrieveToken(AbstractOAuthProvider.java:177) 
04-20 18:01:33.676: W/oauth fail(1099):  ... 13 more 

回答

1

我认为这可能是相关但我没有看到清单文件中的Internet权限

+0

哇我不敢相信那是错误!谢谢一堆 – 2012-04-20 23:32:17