The TwitterCore Kit provides Login with Twitter and the Twitter API
要讓Android實現Twitter Login功能,可以透過twitter-core來達成。
一、在使用Twitter-Core 之前,先來這裡註刪一個帳號
https://get.fabric.io/
二、然候在Android Studio 安裝fabric插件,詳細說明如下網址
https://www.fabric.io/downloads/android-studio
安裝完插件後,重新啟動Android Studio
三、點擊畫面上的Fabric按鈕,登入你的Fabric帳密後,選擇Twitter會有
Sample Code ,以及幫你改變build.gradle ,但是會Crash,我把爬文後
修改的地方貼出來。
四、在build.gradle(Module: app) 長這個樣子
buildscript { repositories { maven { url 'https://maven.fabric.io/public' } } dependencies { classpath 'io.fabric.tools:gradle:1.+' } } apply plugin: 'com.android.application' apply plugin: 'io.fabric' repositories { maven { url 'https://maven.fabric.io/public' } } android { compileSdkVersion 23 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.example.flowercatswets.twitterlogin" minSdkVersion 15 targetSdkVersion 23 versionCode 1 versionName "1.0" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } // Must occur after the Android plugin apply plugin: 'io.fabric' dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.1.1' compile('com.twitter.sdk.android:twitter:1.11.0@aar') { transitive = true; } }
五、Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.flowercatswets.twitterlogin" > <application android:allowBackup="true" android:name=".MyApp" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="io.fabric.ApiKey" android:value="4456b845347338cc3f4d159551f57657a6f683fd" /> </application> <uses-permission android:name="android.permission.INTERNET" /> </manifest>
六、MyApp.java(主要修改的地方放在這裡)
package com.example.flowercatswets.twitterlogin; import android.app.Application; import com.twitter.sdk.android.Twitter; import com.twitter.sdk.android.core.TwitterAuthConfig; import io.fabric.sdk.android.Fabric; /** * Created by flowercatswets on 2016/1/3. */ public class MyApp extends Application { private static final String TWITTER_KEY = "Your KEY"; private static final String TWITTER_SECRET = "Your Secret"; private TwitterAuthConfig authConfig; @Override public void onCreate() { super.onCreate(); authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.with(this, new Twitter(authConfig)); } }
七、MainActivity.java
import android.content.Intent; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.util.Log; import android.widget.Toast; import com.twitter.sdk.android.core.Callback; import com.twitter.sdk.android.core.Result; import com.twitter.sdk.android.core.TwitterException; import com.twitter.sdk.android.core.TwitterSession; import com.twitter.sdk.android.core.identity.TwitterLoginButton; public class MainActivity extends ActionBarActivity { // Note: Your consumer key and secret should be obfuscated in your source code before shipping. private TwitterLoginButton loginButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); loginButton = (TwitterLoginButton) findViewById(R.id.twitter_login_button); loginButton.setCallback(new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { // The TwitterSession is also available through: // Twitter.getInstance().core.getSessionManager().getActiveSession() TwitterSession session = result.data; // TODO: Remove toast and use the TwitterSession's userID // with your app's user model String msg = "@" + session.getUserName() + " logged in! (#" + session.getUserId() + ")"; Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show(); } @Override public void failure(TwitterException exception) { Log.d("TwitterKit", "Login with Twitter failure", exception); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); // Make sure that the loginButton hears the result from any // Activity that it triggered. loginButton.onActivityResult(requestCode, resultCode, data); } }
一開始把這段code放在OnCreate()會Crash,後來爬文後,移到Application後
就正常了。
authConfig = new TwitterAuthConfig(TWITTER_KEY, TWITTER_SECRET); Fabric.with(this, new Twitter(authConfig));
KEY以及SECRET的內容,皆是有Fabric的插件幫我產生出來的,經實測是可以用的。
private static final String TWITTER_KEY = "Your KEY"; private static final String TWITTER_SECRET = "Your Secret";
參考來源:
https://docs.fabric.io/android/twitter/twitter-core.html
http://stackoverflow.com/questions/26629838/twitter-fabric-login-for-android
http://stackoverflow.com/questions/29363372/adding-twitter-kit-to-andoid-studio-project
沒有留言:
張貼留言