QQ三方登录流程总结

Posted by lizubing1992 on 03-01,2019

1.导入相关的jar包

进官方文档可以查看
我导入的是open_sdk_r6008_lite.jar

之后在build.gradle文件加入中

 implementation files('libs/open_sdk_r6008_lite.jar')

2.初始化Tencent类并吊起QQ界面

在onCreate中初始化

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //传入参数APPID和全局Context上下文
        tencent = Tencent.createInstance(QQ_APP_ID, this.getApplicationContext());
    }
//吊起QQ登录界面
 qqLoginTV.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (!tencent.isSessionValid()) {
          tencent.login(LoginActivity.this, "all", new BaseUiListener());
        }
      }
    });

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    LogUtils.w("onActivityResult----------");
    Tencent.onActivityResultData(requestCode, resultCode, data, new BaseUiListener());
    if(requestCode == Constants.REQUEST_API) {
      if(resultCode == Constants.REQUEST_LOGIN) {
        Tencent.handleResultData(data, new BaseUiListener());
      }
    }
    super.onActivityResult(requestCode, resultCode, data);
  }

对应的BaseUiListener

private class BaseUiListener implements IUiListener {

    @Override
    public void onComplete(Object response) {
      MyApplication.showToast("登录成功");
      try {
        //获得的数据是JSON格式的,获得你想获得的内容
        LogUtils.v("-------------"+response.toString());
        String openidString = ((JSONObject) response).getString("openid");
        String expires_in = ((JSONObject) response).getString("expires_in");
        String access_token= ((JSONObject) response).getString("access_token");
        tencent.setOpenId(openidString);
        tencent.setAccessToken(access_token,expires_in);
        showLoadProgress("登录中!",false);
        mPresenter.thirdQqLogin(access_token,openidString);
      } catch (JSONException e) {
        e.printStackTrace();
      }
    }

    @Override
    public void onError(UiError uiError) {
      LogUtils.w("onError");
    }

    @Override
    public void onCancel() {
      LogUtils.w("onCancel");
    }
  }

BaseUiListener中的onComplete方法返回对应的数据信息,后台一般只要access_token和openid这两个参数即可

3.配置文件AndroidManifest.xml

 <activity
      android:launchMode="singleTask"
      android:name="com.tencent.tauth.AuthActivity"
      android:noHistory="true">
      <intent-filter>
        <action android:name="android.intent.action.VIEW" />

        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />

        <data android:scheme="QQ_APP_ID" />
      </intent-filter>
    </activity>
    <activity
      android:configChanges="orientation|keyboardHidden|screenSize"
      android:name="com.tencent.connect.common.AssistActivity"
      android:theme="@android:style/Theme.Translucent.NoTitleBar" />

QQ_APP_ID是对应从QQ开放平台中申请的appID

一般按照文档来,基本没啥事情,开放平台上面配置时需要对应的包名和签名要正确


0评论