ZeroyiQ: Unity multi-platform native SDK access Overview (I) : wechat open platform

ZeroyiQ: Unity multi-platform native SDK access overview (ii) : QQ Interconnection

ZeroyiQ: Unity multi-platform native SDK Access Quick overview (3) : Facebook

ZeroyiQ: Unity multi-platform native SDK Access Overview (4) : Twitter

ZeroyiQ: Unity multi-platform native SDK access overview (five) : Weibo

One, foreword

It’s been almost a week since the last update, not me cooing, just something.

Twitter access current (2020-7-10) is also required to apply for a developer account, create an application to obtain key and Secret. Note that the Twitter developer account is said to be not easy to apply for and cannot be re-applied for once it fails. And then I took a chance, and I ran out of seconds, XD. (Explore the APIs, location: HK, +86 mobile phone number, Gmail, no data display, no data analysis, reason: to complete homework)

When I got to the next step — looking for the official SDK for Android — I couldn’t find the official documentation and found out that Twitter had stopped maintaining the SDK for 18 years. It took some effort to find a good alternative, but after a lot of searching, I found that the most correct way is to implement a web-based version of Twitter login. However, due to my lack of understanding of the Web and time factor, I prefer to use the SDK that Twitter officially stopped maintaining for the time being. Through Twitter Kit for Unity, it is verified that the current SDK can still authorize login normally. So I chose to plug in Twitter Kit for Android.

Since I got the Key and Secret, I went through the whole process thoroughly. Compile aar package to import Unity project, configure AndroidManifest and Gradle, and then export the project to Android Studio to compile into APK. There will be a follow-up article.

2. SDK access

1. Configure dependencies

build.gradle[Project]

repositories {
        jcenter()
    }
Copy the code

build.gradle[Module]

Dependencies {implementation 'com. Twitter. SDK. The android: twitter: 3.1.1'}Copy the code

2. The initialization

Initialize Twitter.

private boolean bIsInitialized; private TwitterAuthClient authClient; private void init() { if (! bIsInitialized) { bIsInitialized = true; String key = activity.getString(R.string.twitter_app_key); String secret = activity.getString(R.string.twitter_app_secret); UnityCallApi.unityLogInfo(TAG, "key: " + key + " secret: " + secret); TwitterConfig config = new TwitterConfig.Builder(activity) .logger(new DefaultLogger(Log.DEBUG)) .twitterAuthConfig(new TwitterAuthConfig(key, secret)) .debug(false) .build(); Twitter.initialize(config); AuthClient = new TwitterAuthClient(); }}Copy the code

Third, the login

1. Twitter’s native button

1. A Add Button layout ADDED in XML.

<com.twitter.sdk.android.core.identity.TwitterLoginButton
     android:id="@+id/login_button"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content" />
Copy the code

1. B initiates a request

loginButton = (TwitterLoginButton) findViewById(R.id.login_button); loginButton.setCallback(new Callback<TwitterSession>() { @Override public void success(Result<TwitterSession> result) { Override public void failure(TwitterException exception) {// Failed callback}Copy the code

1. C Set callback

Override // This code is very important, Protected void onActivityResult(int requestCode, int resultCode, Intent data) {/ / passed callback loginButton onActivityResult (requestCode, the resultCode, data); super.onActivityResult(requestCode, resultCode, data); }Copy the code

2. Customize buttons

2. User A initiates a request

Trigger the login request directly through the Authorize method.

/ / authClient already in the second. 2 initialize the enclosing authClient. The authorize (activity, New Callback<TwitterSession>() {public void success(Result<TwitterSession> Result) {// Successfully Callback TwitterSession data = (TwitterSession) result.data; UnityCallApi.unityLogInfo(TAG, "Login successful. " + data.getUserName() + " " + data.getUserId()); UnityCallApi.sendLoginInfoToUnity(true, result.data.toString()); } public void failure (TwitterException ex) {/ / callback UnityCallApi failure. UnityLogError (TAG, "Login error. " + ex.getMessage()); UnityCallApi.sendLoginInfoToUnity(false, ""); }});Copy the code

2. B Set callback

Override // This code is very important, Protected void onActivityResult(int requestCode, int resultCode, Intent data) {/ / passed callback authClient onActivityResult (requestCode, the resultCode, data); super.onActivityResult(requestCode, resultCode, data); }Copy the code

4. Obtain user information

The TwitterSession contains basic user information (USERNAME, ID).

Session = TwitterCore.getInstance().getsessionManager ().getActivesession (); String userName = session.getUserName(); String userId = session.getUserId(); TwitterAuthToken authToken = session.getAuthToken(); String token = authToken.token; String secret = authToken.secret;Copy the code

Five, share,

Build. Gradle [Module] adds dependencies

Dependencies {implementation 'com. Twitter. SDK. The android: twitter: 3.1.1' / / new implementation 'com. Twitter. SDK. The android: tweet - composer: 3.1.1'}Copy the code

Making a share request

public void shareImage(Bundle params) { TwitterSession session = TwitterCore.getInstance().getSessionManager().getActiveSession(); if (session ! = null) { String imagePath = params.getString(PlatConstants.SHARE_IMAGE_PATH_STRING); String text = params.getString(PlatConstants.SHARE_TEXT_STRING); Intent intent = (new com.twitter.sdk.android.tweetcomposer.ComposerActivity.Builder(activity)) .session(session) Image (" image Uri "). The text (" share content "). Hashtags (" # label "). CreateIntent (); activity.startActivity(intent); }}Copy the code

Create a new callback class, TwitterResultReceiver inherits BroadcastReceiver

public class TwitterResultReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, {if Intent Intent) (Intent. GetAction () equals (TweetUploadService. UPLOAD_SUCCESS)) {String text = "share success!" ; Toast.makeText(context,text,Toast.LENGTH_SHORT).show(); }else {String text =" Share failed!" ; Toast.makeText(context,text,Toast.LENGTH_SHORT).show(); }}}Copy the code

Add Receiver in androidmanifest.xml

<receiver Android :name=" replace with TwitterResultReceiver path "Android: Exported ="false"> <intent-filter> <action android:name="com.twitter.sdk.android.tweetcomposer.UPLOAD_SUCCESS" /> <action android:name="com.twitter.sdk.android.tweetcomposer.UPLOAD_FAILURE" /> <action android:name="com.twitter.sdk.android.tweetcomposer.TWEET_COMPOSE_CANCEL"/> </intent-filter> </receiver>Copy the code

Six, summarized

Currently, although Twitter has officially stopped the maintenance of SDK, login and sharing can be used normally at present (2020-7-13), so you can access SDK for use first, and modify SDK directly if there is any change. Of course, if you can package a web version of the implementation, this should be the right way to use. Please choose your options accordingly.

Seven, references,

  1. Twitter Development Platform
  2. How do I get a Twitter developer account?
  3. Twitter stopped maintaining the SDK
  4. QQ MSDK Twitter channel access document
  5. Twitter kit for Android
  6. Twitter kit for Unity
  7. Twitter kit for Website