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

The official platform to apply for a developer account, simple filling can be, current (2020-7-17) individual developers can quickly apply down. Create an application and obtain the AppKey. Note that you must fill in the Android package name and signature to properly authorize. Enter the location in [Application Info] – [Basic Info] on the application interface.

  • Get the Android package name from the applicationId value in the build.gradle file of the project main modules (apply plugin = ‘com.android.application’).
  • Android signature obtaining method: The SDK integration tool includes the application APK for obtaining the signature. You can obtain the signature of the application by the package name. Check out this blog for more information on signatures

2. SDK access

1. Configure dependencies

build.gradle[Project]

allprojects {
    repositories {
        maven { url "https://dl.bintray.com/thelasterstar/maven/" }
    }
}
Copy the code

build.gradle[Module]

android { defaultConfig { ndk { abiFilters 'armeabi' //, 'armeabi - v7a' dependencies' arm64 - v8a '}}} {implementation 'com, sina weibo. The SDK: core: 10.7.0: openDefaultRelease @ aar'}Copy the code

2. Add permissions

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Copy the code

3. The initialization

Public static final String wb_appkey = "AppKey"; Public static final String WB_REDIRECT_RUL = "http://www.sina.com"; Public static final String WB_USER_SCOPE = "email,direct_messages_read,direct_messages_write,"; Private void init() {AuthInfo AuthInfo = new AuthInfo(Activity, WB_APP_KEY, WB_REDIRECT_RUL, WB_USER_SCOPE); private void init() {AuthInfo AuthInfo = new AuthInfo(Activity, WB_APP_KEY, WB_REDIRECT_RUL, WB_USER_SCOPE); wbApi = WBAPIFactory.createWBAPI(activity); wbApi.registerApp(activity, authInfo); }Copy the code

Three login

To initiate a login authorization request, you can choose to authorize the login only through the web page or the client, or you can ask the SDK to select the authorization mode automatically.

Private void startAuth(){// Automatically select authorization (web page or client) wbapi.authorize (new WbAuthListener() {@override public void onComplete(Oauth2AccessToken oauth2AccessToken) { UnityCallApi.unityLogInfo(TAG, "Login successful."); AccessTokenHelper.writeAccessToken(activity, oauth2AccessToken); / / save the TOKEN UnityCallApi. SendLoginInfoToUnity (true, the String. Format (username: id: % s" %s ", oauth2AccessToken.getUid(), oauth2AccessToken.getScreenName())); } @Override public void onError(UiError uiError) { UnityCallApi.unityLogError(TAG, "Login error." + uiError.errorCode + uiError.errorCode); UnityCallApi.sendLoginInfoToUnity(false, ""); } @Override public void onCancel() { UnityCallApi.unityLogInfo(TAG, "Login cancel."); }}); AuthorizeClient (new WbAuthListener()){//... AuthorizeWeb (new WbAuthListener()){//... / /}}Copy the code

Set the authorization callback to override the Activity’s OnActivityResult.

@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (wbApi ! = null) { wbApi.authorizeCallback(requestCode, resultCode, data); } super.onActivityResult(requestCode, resultCode, data); }Copy the code

4. Obtain user information

The basic Uid and ScreenName of the user are already included in the return information of the authorized login.

If you need more detailed information, you need to apply for verification by the official SDK maintenance personnel. We know that API (10.7.0) no longer provides the interface to obtain user information directly (userapi.Show), and you need to send GET requests according to the document.

Five, share,

Implement the share callback interface

private static class ShareCallback implements WbShareCallback { @Override public void onComplete() { UnityCallApi.unityLogInfo(TAG, "shared successful."); } @Override public void onError(UiError uiError) { UnityCallApi.unityLogError(TAG, String.format("shared error. code:%s msg:%s", uiError.errorCode, uiError.errorMessage)); } @Override public void onCancel() { UnityCallApi.unityLogInfo(TAG, "shared cancel."); }}Copy the code

Set the share callback to override the Activity’s OnActivityResult.

@Override protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { if (wbApi ! = null) { wbApi.authorizeCallback(requestCode, resultCode, data); // ShareCallback wbapi.doresultintent (data, new ShareCallback()); } super.onActivityResult(requestCode, resultCode, data); }Copy the code

1. The word

public void shareText(Bundle params) { TextObject textObject = new TextObject(); Textobject. text = "Share text content "; WeiboMultiMessage message = new WeiboMultiMessage(); message.textObject = textObject; boolean isOnlyClient = false; Wapi. ShareMessage (message, isOnlyClient); }Copy the code

Picture 2.

public void shareImage(Bundle params) { ImageObject imageObject = new ImageObject(); Bitmap data = // ImageObject.setimageData (data); WeiboMultiMessage message = new WeiboMultiMessage(); message.imageObject = imageObject; wbApi.shareMessage(message, false); }Copy the code

3. The web page

public void shareWebLink(Bundle params) { WebpageObject webpageObject = new WebpageObject(); webpageObject.identify = UUID.randomUUID().toString(); Webpageobject. title = "title "; WebpageObject. The description = "description"; ActionUrl = "webpageObject "; WebpageObject. DefaultText = "share page"; WeiboMultiMessage message = new WeiboMultiMessage(); message.mediaObject = webpageObject; wbApi.shareMessage(message, false); }Copy the code

Six, summarized

The SDK for Weibo is surprisingly well packaged and easy to use. The official SDK repository is a bit messy. The 2019 SDK folder should be the latest SDK10.7.0. The PDF document in it is missing due to the export problem. So it’s best to use the official Demo as a reference.

Seven, references,

  1. Weibo open platform
  2. Official SDK Repository
  3. Weibo apis
  4. Description of the official document Scope