Last active
August 29, 2015 13:56
-
-
Save niusounds/9130062 to your computer and use it in GitHub Desktop.
Twitter4jとAndroidAnnotationsによるTwitter認証ダイアログの実装
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TwitterAuthDialogFragment dialog = TwitterAuthDialogFragment_.builder() | |
.consumerKey("xxxxx") | |
.consumerSecret("yyyyy") | |
.callbackUrl("http://xxxxx/yyyy") | |
.build(); | |
dialog.setCallbacks(new TwitterAuthDialogFragment.Callbacks() { | |
@Override | |
public void onAccessTokenAvailable(String accessToken, String accessTokenSecret) { | |
// save accessToken & accessTokenSecret or do something | |
} | |
}); | |
dialog.show(getFragmentManager(), null); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.androidannotations.annotations.Background; | |
import org.androidannotations.annotations.EFragment; | |
import org.androidannotations.annotations.FragmentArg; | |
import org.androidannotations.annotations.UiThread; | |
import twitter4j.Twitter; | |
import twitter4j.TwitterException; | |
import twitter4j.TwitterFactory; | |
import twitter4j.auth.AccessToken; | |
import twitter4j.auth.RequestToken; | |
import twitter4j.conf.ConfigurationBuilder; | |
import android.app.AlertDialog; | |
import android.app.Dialog; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.support.v4.app.DialogFragment; | |
import android.webkit.WebView; | |
import android.webkit.WebViewClient; | |
@EFragment | |
public class TwitterAuthDialogFragment extends DialogFragment { | |
public static interface Callbacks { | |
void onAccessTokenAvailable(String accessToken, String accessTokenSecret); | |
} | |
private WebView view; | |
private RequestToken requestToken; | |
private Callbacks callbacks; | |
@FragmentArg | |
String consumerKey; | |
@FragmentArg | |
String consumerSecret; | |
@FragmentArg | |
String callbackUrl; | |
@Override | |
public Dialog onCreateDialog(Bundle savedInstanceState) { | |
view = new WebView(getActivity()) { | |
@Override | |
public boolean onCheckIsTextEditor() { | |
return true; | |
} | |
}; | |
view.setWebViewClient(new WebViewClient() { | |
@Override | |
public boolean shouldOverrideUrlLoading(WebView view, String url) { | |
if (url.contains(callbackUrl)) { | |
Uri uri = Uri.parse(url); | |
String verifier = uri.getQueryParameter("oauth_verifier"); | |
if (verifier != null) { | |
getAccessToken(verifier); | |
} | |
return true; | |
} | |
return false; | |
} | |
}); | |
return new AlertDialog.Builder(getActivity()) | |
.setView(view) | |
.create(); | |
} | |
@Override | |
public void onStart() { | |
super.onStart(); | |
init(); | |
} | |
@Background | |
void init() { | |
try { | |
Twitter twitter = TwitterFactory.getSingleton(); | |
if (consumerKey != null && consumerSecret != null) { | |
twitter.setOAuthConsumer(consumerKey, consumerSecret); | |
} | |
requestToken = twitter.getOAuthRequestToken(); | |
String authUrl = requestToken.getAuthorizationURL(); | |
loadUrl(authUrl); | |
} catch (TwitterException e) { | |
e.printStackTrace(); | |
dismiss(); | |
} | |
} | |
@Background | |
void getAccessToken(String verifier) { | |
try { | |
AccessToken accessToken = new TwitterFactory(new ConfigurationBuilder().build()).getInstance().getOAuthAccessToken(requestToken, verifier); | |
callback(accessToken.getToken(), accessToken.getTokenSecret()); | |
dismiss(); | |
} catch (TwitterException e) { | |
e.printStackTrace(); | |
} | |
} | |
@UiThread | |
void callback(String token, String tokenSecret) { | |
if (callbacks != null) { | |
callbacks.onAccessTokenAvailable(token, tokenSecret); | |
} | |
} | |
@UiThread | |
void loadUrl(String url) { | |
view.loadUrl(url); | |
} | |
@Override | |
@UiThread | |
public void dismiss() { | |
super.dismiss(); | |
} | |
public void setCallbacks(Callbacks callbacks) { | |
this.callbacks = callbacks; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment