ãããå®éãååã®ãããžã§ã¯ãã§ã¯ããŠãŒã¶ãŒã«FacebookãTwitterã§é²è¡ç¶æ³ã«é¢ããã¡ãã»ãŒãžïŒããã³åçïŒããã€ãºãã³ã°ããæ©äŒãæäŸããªããã°ãªããŸããã§ããã ãã®èšäºãæžãåã«ãå ±æãæ åœããã³ãŒãã®éšåãå¥ã®ã³ã³ããŒãã³ãã«åããŠãFacebook APIãšTwitter APIã§ã®äœæ¥ãå®èšŒããããšã«ããŸããã ãããããã®åŸãOstapã¯èŠãã¿ããã®çµæãFacebookãšTwitterïŒãããŠå°æ¥ãä»ã®ãœãŒã·ã£ã«ãããã¯ãŒã¯ïŒã§ç°¡åã«å ±æããããã®åå©çšå¯èœãªããŒã«ãå€æããŸããã ãåçŽãªå ±æããšã¯ãåã«ã¡ãã»ãŒãžãŸãã¯åçãéä¿¡ããæ©èœãæå³ããŸãã åçŽãªå ±æã¯ããããªãŒãºããŸãã¯ããªãã€ãŒãããæå³ããŸããã ã·ã³ãã«ãªæ©èœ-ã·ã³ãã«ãªã€ã³ã¿ãŒãã§ãŒã¹ïŒ APIã䜿çšããéã®ãã¹ãŠã®å°é£ãçŸãããã¡ãµãŒãã®èåŸã«é ãããšããŸããã ããããããã«ã€ããŠã¯åŸã§...
ã ãããç§ãã¡ã«ã¯ã¿ã¹ã¯ããããŸãïŒããªãã«ä»£ãã£ãŠFacebookã®ãŠã©ãŒã«ã«ã¡ãã»ãŒãžãéä¿¡ããTwitterã§ããªãã®ã¹ããŒã¿ã¹ãæŽæ°ããããšã§ãã ãã¡ããã¡ã㧠ã OAuthã䜿çšããŠæ¿èªãè¡ãããŸãïŒOAuthèªèšŒã®åçã¯ã ãã®å³ã«ããåæ ãããŠããŸã ïŒã ãŸãããªãã£ã¹ã«ç»ããŸãã æºåãã§ããŠãããã®ãæ¢ããŠãããµã€ãã
ãã§ã€ã¹ããã¯
Facebookã¯ãGitHub'eã§å ¥æã§ããå ¬åŒã®Android SDKãããã³ã¢ããªã±ãŒã·ã§ã³ã§ãã®SDKã®äœ¿çšãéå§ããããã®è©³çŽ°ãªæé ã«æºè¶³ããŠããŸãã SDKã®ãã¹ãŠãéåžžã«åçŽã§ãããšã¯èšããŸããïŒäžéšã®ãã©ã¡ãŒã¿ãŒã¯è³ªåãçºçãããŸãïŒããæ¿èªã¿ã¹ã¯ãå®å šã«å®è¡ããŸããåŒã³åºãå¿ èŠãããã®ã¯ã
æ¢è£œã®ãã€ã¢ãã°èªäœã衚瀺ããããŠãŒã¶ãŒãWebãã©ãŒã ã«ããŒã¿ãå ¥åã§ããããã«ããæçµçã«ã¯Facebook APIãšã®ãããªãéä¿¡ã«å¿ èŠãªèªèšŒããŒã¯ã³ãè¿ããŸãã ããã§ã¯ãããŸããŸãªããªãšãŒã·ã§ã³ã§æ¢ã«requestïŒ...ïŒã¡ãœããã䜿çšã§ããã¯ãã§ãã ããšãã°ãå£ã«ã¡ãã»ãŒãžãéä¿¡ããã«ã¯ã次ã®ããã«åé¯ããå¿ èŠããããŸããfacebook.authorize(Activity activity, String[] permissions, DialogListener listener);
Bundle params = new Bundle(); params.putString("message", "Android Yorshik is great!"); facebook.request("me/feed", params, "POST");
ã©ããªãèªå/ãã£ãŒãããªã®ããç解ããã«ã¯ãFacebookã®Open Graphã³ã³ã»ããã浞éãããå¿ èŠããããŸãã ããããããã¯æé ãªäŸ¡æ Œã®Facebook APIã®ãã«ãã¯ãŒã䜿çšãããå Žåã®ã¿ã§ãã
æåã®æ°Žäžã®å°ç³ã¯ããã§äŒããŸããïŒFacebookã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ãããã€ã¹ã«æ¢ã«ã€ã³ã¹ããŒã«ãããŠããå Žåãæ¿èªãªã¯ãšã¹ãfacebook.authorizeïŒ...ïŒã¯ãå éšã®WebViewãšã®ãã€ã¢ãã°ã®è¡šç€ºã«ã€ãªãããŸãããããã®ãŸãã«ã¯ã©ã€ã¢ã³ãã¢ããªã±ãŒã·ã§ã³ã®èµ·åãšãã®ãã°ã€ã³ãã©ãŒã ãéããŸãã èšç»ã«ãããšãæ¿èªåŸããã€ã¢ãã°ã®åŸãšåãæ¹æ³ã§ã¢ããªã±ãŒã·ã§ã³ã«æ»ããŸãã ãããã onResumeïŒïŒãŸãã¯onStopïŒïŒã§äœããçªç¶å·§åŠã«çµã³ä»ããããå Žåãããããåã³åŒã³åºãããå¯èœæ§ãããããšãèæ ®ããå¿ èŠããããŸãã 1ã€ã®ããã€ã¹ã§ã¯ããã®åœ¢åŒã®æ¿èªã¯äžè¬ã«ãšã«ããæ©èœããŸããã ãã®ããã念ã®ãããDialogãä»ããŠåžžã«ãã°ã€ã³ããããšæããŸãã ãããŠããã®ãããªæ©äŒããããŸãïŒ Facebook.FORCE_DIALOG_AUTHã®è¿œå ãã©ã¡ãŒã¿ãŒãããã«åœ¹ç«ã¡ãŸãã ç§ãã¡ã¯é»è©±ããŸãïŒ
察話ãéããŠæ¿èªãå®å®ããŸããfacebook.authorize(context, permissions, Facebook.FORCE_DIALOG_AUTH, authListener);
ããããTwitterã¯éçºè ã®å ¬åŒSDKãæäŸããŸããã§ããã ããããããŸããŸãªæ©èœãåããéå ¬åŒã®ãã®ããããããããŸãã èæ ®ã«å ¥ããæåã®äºæïŒ
- Sugree Twitter Android SDKã¯ãåºæ¬çã«Twitter APIã«åãããFacebook SDKã¯ããŒã³ã§ãã ãã ãã圌ã«ã§ããããšã¯ããŠãŒã¶ãŒèªèšŒãæäŸããããšã ãã§ãïŒãã ããå°ãªããšããŠãŒã¶ãŒãã°ã€ã³çšã®æ¢è£œã®UIããããŸãïŒ
- Twitter4J-匷åãªã©ã€ãã©ãªã»ãããTwitterãæäœããããã®åºç¯ãªæ©èœïŒUIãªãïŒ
- JTwitter -Twitter4Jã»ã©åºç¯ã§ã¯ãªãããååãªæ©èœïŒUIãªãïŒ
ã©ã€ãã©ãªããã¹ãããåŸãTwitter4JãšSugreeãããžã§ã¯ãã®UIéšåã暪æããããšã決å®ãããŸããã è©Šè¡é¯èª€ã«ããã2ã€ã®èªèšŒæ¹æ³ãçºèŠãããŸããã
1ïŒæåã®ã±ãŒã¹ïŒå®è£ ããŒã«ã§äœ¿çšãããæç¹ïŒã§ã¯ã dev.twitter.comã«ã¢ããªã±ãŒã·ã§ã³ãç»é²ãããšãã«ã ãCallback URLããã£ãŒã«ãã空ã®ãŸãŸã«ããŸãã 次ã«ããŠãŒã¶ãŒãèªèšŒããŒã¿ãå ¥åããŠ[èªèšŒ]ãã¯ãªãã¯ãããšãPINã³ãŒããèšèŒãããããŒãžã«ãªãã€ã¬ã¯ããããŸããPINã³ãŒãã¯äžè¬çã«ã©ãã«å ¥åãããæ確ã§ã¯ãããŸããã ãããããããå¿ èŠãšããã®ã¯ããããTwitterã®ãµãŒãã¹ã«è»¢éããããšã«ãã£ãŠã®ã¿ããããŒã¯ã³ãšç§å¯ãã®èªèšŒãååŸã§ããããã§ãã ãã®çš®ã®ã³ãŒããå ¥åããŠããäžåºŠãŠãŒã¶ãŒãèŠãããããšã¯ããããããä»äºã§ãã ãããã£ãŠãHTMLããŒãžãååŸããŠè§£æãããã®PINã³ãŒããããããåãåããŸãã WebViewã«ããŒããããããŒãžã®HTMLã³ãŒããååŸããæ¹æ³ã¯ïŒ ãŸã£ããè€éã§ã¯ãããŸããïŒ
- ããŒãžã®HTMLã³ãŒããæ¥ãJavaScriptInterfaceãäœæããŸã
class MyJavaScriptInterface { public void processHTML(String html) { // process HTML } }
- ãã®ãã€ã³ã¿ãŒãã§ãŒã¹ããWebViewã«è¿œå ããŸã
webView.getSettings().setJavaScriptEnabled(true); webView.addJavascriptInterface(new MyJavaScriptInterface(), "HTMLOUT");
- æåŸã«ãããŒãžãå®å
šã«ããŒããããåŸã«JavaScriptã³ãŒããå®è¡ããŸã
webView.setWebViewClient(new MyWebViewClient()); ... private class MyWebViewClient extends WebViewClient { ... @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); browser.loadUrl("javascript:window.HTMLOUT.processHTML('<head>'+document.getElementsByTagName('html')[0].innerHTML+'</head>');"); } }
以äžã§ãPINã³ãŒããæœåºããŠèªèšŒãå®äºããŸããã
2ïŒãããŠä»ãæ£ããæ¹æ³ïŒãã£ãŒã«ãã®dev.twitter.comã®ã¢ããªã±ãŒã·ã§ã³ã®ããããã£ã§
ãã³ãŒã«ããã¯URLã㯠ãæå¹ãªURLïŒ ãhttpïŒ//your.shikããªã© ïŒãæ¿å ¥ããŸãã ãããŠä»ãããŒãžãPINã³ãŒãã§è§£æãã代ããã«ãTwitterããã³ãŒã«ããã¯ããã£ããããå¿ èŠããããŸãããã®URLã«ã¯oauth_verifierãã©ã¡ãŒã¿ãŒãå«ãŸããåããããŒã¯ã³ãšã·ãŒã¯ã¬ããããååŸããããã«PINã³ãŒãã®ä»£ããã«äœ¿çšããŸãã ãã®ãããªååã¯ãã®ãããžã§ã¯ãã§æåè£ã«å®è£ ãããŠãããè¿ãå°æ¥ãèªå® ã§åãããžãã¯
ã·ã³ãã«ãªãœãŒã·ã£ã«å ±æ
Simple Social SharingãšåŒã°ãããäœæããããããžã§ã¯ãã«é¢ããããã€ãã®èšèã ãã§ã«åé ã§è¿°ã¹ãããã«ããããžã§ã¯ãã®äž»ãªç®çã¯ãFacebookãTwitterïŒå°æ¥çã«ã¯ä»ã®ãœãŒã·ã£ã«ãããã¯ãŒã¯ïŒãšç°¡åã«å ±æã§ããã·ã³ãã«ãªAPIãæäŸããããšã§ãã åçŽãªAPIã«ã¯4ã€ã®ïŒ+1ïŒã¡ãœãããå«ãŸããŸãã
- isAuthorizedïŒïŒ -ãŠãŒã¶ãŒãæ¿èªãããçŸåšã®ã»ãã·ã§ã³ãæå¹ãã©ããã確èªããŸã
- authorizeïŒïŒ -ãŠãŒã¶ãŒããã°ã€ã³ãããã€ã¢ãã°ãåŒã³åºããŸã
- publishMessageïŒ...ïŒ -ã¡ãã»ãŒãžãå
¬éããŸã
- publishImageïŒ...ïŒ -ç»åãå ¬éããŸãïŒFacebookã®ã¿ïŒ
- logoutïŒïŒ -çŸåšã®ã»ãã·ã§ã³ãã¯ãªã¢ããä¿åããããããŒã¯ã³ãšã·ãŒã¯ã¬ããããåé€ããŸã
ãã€ã¢ãã°ã¯æ¬¡ã®ããã«ãªããŸãã
ä»äºã«å¿ èŠãªãã®ã¯æ¬¡ã®ãšããã§ãã
- äžèšã®æ¹æ³ãæäŸããFacebookFacadeããã³TwitterFacadeãã¡ãµãŒãã 次ã®ããã«äœæãããŸããã
FacebookFacade facebook = new FacebookFacade(activity, FACEBOOK_APP_ID); TwitterFacade twitter = new TwitterFacade(context, TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET);
- æ¿èªã€ãã³ãïŒ onAuthSucceedïŒïŒããã³onAuthFailïŒïŒ ïŒãã¡ãã»ãŒãžã®å ¬éïŒ onPostPublishedïŒïŒããã³onPostPublishingFailedïŒïŒ ïŒãããã³ãã°ã¢ãŠãïŒ onLogoutCompleteïŒïŒ ïŒã®ãµãã¹ã¯ãªãã·ã§ã³ãåãå ¥ããFacebookEventsããã³TwitterEventsã¢ã©ãŒãã»ã³ã¿ãŒã ãªã¹ããŒãè¿œå ã§ããŸãããåŸã§åé€ããããšãå¿ããªãããšãéèŠã§ãã
ããããã次ã®FacebookFacadeã¡ãœãããåé¡ãæèµ·ããŸãã
publishMessage(String message, String link, String linkName, String linkDescription, String pictureUrl, Map<String, String> actions)
çµæã®ã³ãŒããšå³ã¯ããã¹ãŠã説æãããšæããŸãã
String message = "Look at this great App!"; String link = "https://github.com/nostra13/Android-Simple-Social-Sharing"; String linkName = "Use Android Simple Social Sharing in your project!"; String linkDescription = "Also see other projects of nostra13 on GitHub!"; String pictureUrl = "http://cdn.androidcommunity.com/wp-content/uploads/2011/01/facebook-android-logo-1.jpg"; Map<String, String> actions = new HashMap<String, String>(); actions.put("Android Simple Social Sharing", "https://github.com/nostra13/Android-Simple-Social-Sharing"); facebook.publishMessage(message, link, linkName, linkDescription, pictureUrl, actions);
ããã ãã§ãã ãããžã§ã¯ãã®åœé¢ã®èšç»ã¯ãã³ãŒã«ããã¯ã䜿çšããããã®æ¿èªãããçŽããéãTwitter4Jã®äœ¿çšãå®å šã«åãé€ãããšã§ãã ããŠãæã ã¯è¡šç€ºãããŸãã
ãã€ãã®ããã«-GitHubã®ãœãŒã¹ã³ãŒã ã ãã®ããŒã«ãããªãã«ãšã£ãŠã圹ã«ç«ã€ããšãé¡ã£ãŠããŸãã ä»ã®ãœãŒã·ã£ã«ãããã¯ãŒã¯ãžã®ãµããŒããè¿œå ããããã®ã¢ããªã±ãŒã·ã§ã³ãåãå ¥ããããŸãïŒæ®å¿µãªãããGoogle +ã¯å¥åã®SDKããŸã æäŸããŠããŸããïŒã
UPDïŒåŽåè ã®èŠæ±ã«å¿ããŠã ç¹å®ã®ãœãŒã·ã£ã«ãããã¯ãŒã¯ã®ã¯ã©ã€ã¢ã³ããã¹ããŒããã©ã³ã«ã€ã³ã¹ããŒã«ãããŠããªãã®ã§ããã®ãããã¯ãŒã¯ã§ææ¢ãããããªãå Žåã¯ãæãç°¡åãªå ±ææ¹æ³ã䜿çšã§ããŸãïŒã©ã€ãã©ãªããã蟌ãå¿ èŠã¯ãããŸããïŒã
final Intent intent = new Intent(Intent.ACTION_SEND); intent.setType(«text/plain»); intent.putExtra(Intent.EXTRA_TEXT, "Try Android Yorshik!"); startActivity(Intent.createChooser(intent, getString(R.string.app_name)));
ç°¡åãªå ±æã®äŸã«ã€ããŠã¯ã NikitaGã«æè¬ããŸãã