我正在将 Twitter oAuth 系统用于我的应用程序,特别是遵循本教程: http: //www.markhneedham.com/blog/2012/01/02/learning-android-authenticating-with-twitter-using-oauth /。我似乎收到了基于我的回调 URL 的错误,我只是无法在任何地方找到有关如何正确执行此操作的解决方案或正确信息。我的应用程序的名称是啁啾,生病发布代码和错误,任何帮助将不胜感激!
注意:当我尝试通过 twitter 开发人员在我的应用程序页面上设置它们时,很多教程都有 CALLBACKURL 的随机 Twitter URL,例如“app://twitter”,它们是“无效 URL”。目前,我在我的应用程序的开发人员页面和代码中都将 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