私は自分のアプリにTwitteroAuthシステムを使用しています。具体的には、このチュートリアルに従ってください:http: //www.markhneedham.com/blog/2012/01/02/learning-android-authenticating-with-twitter-using-oauth /。コールバックURLに基づいてエラーが発生しているようですが、これを適切に行う方法に関する解決策や適切な情報がどこにも見つかりません。私のアプリの名前はchirpeeです。コードとエラーを投稿してください。助けていただければ幸いです。
注:多くのチュートリアルには、「app://twitter」のようなCALLBACKURLのランダムなTwitterURLがあります。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