安卓用微信作为第三方登录,登录授权界面调用

在 Android 应用中实现微信第三方登录,可以通过调用微信提供的开放平台 SDK 来完成。以下是实现微信登录授权界面的完整步骤:

1. 准备工作

注册微信开放平台应用

  1. 登录 微信开放平台
  2. 创建应用,获取 AppIDAppSecret
  3. 配置应用信息,包括回调域名。

下载微信 SDK

  1. 前往 微信开放平台下载页,下载 Android SDK
  2. 解压后,将 libs 文件夹中的 .jar 文件和 .so 文件加入项目中。

2. 项目配置

引入微信 SDK

将 SDK 文件添加到项目的 libsjniLibs 目录下:

  • libs 下的 wechat_sdk.jar 添加到项目的 libs 文件夹。
  • lib 下的 armeabi-v7a 等文件夹放入 src/main/jniLibs 目录。

build.gradle 中添加依赖:

implementation fileTree(dir: "libs", include: ["*.jar"])

配置权限

AndroidManifest.xml 中添加权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

注册微信开放平台的 AppID:

<application>
    <meta-data
        android:name="com.tencent.mm.opensdk.APP_ID"
        android:value="your_wechat_app_id" />

    <activity
        android:name=".wxapi.WXEntryActivity"
        android:exported="true"
        android:launchMode="singleTask">
        <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="your_wechat_app_id"
                android:host="oauth" />
        </intent-filter>
    </activity>
</application>

3. 实现微信登录逻辑

创建 WXEntryActivity

在包名路径下创建 wxapi/WXEntryActivity,用于接收微信回调:

package com.example.yourapp.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelmsg.SendAuth;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        api = WXAPIFactory.createWXAPI(this, "your_wechat_app_id", false);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {}

    @Override
    public void onResp(BaseResp resp) {
        if (resp instanceof SendAuth.Resp) {
            SendAuth.Resp authResp = (SendAuth.Resp) resp;
            if (authResp.errCode == BaseResp.ErrCode.ERR_OK) {
                // 获取授权临时票据
                String code = authResp.code;
                // 将 code 发送到服务器,获取 access_token 和 openid
            } else {
                // 用户取消或授权失败
            }
        }
        finish();
    }
}

在 MainActivity 中调用微信登录

import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.Toast;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

public class MainActivity extends AppCompatActivity {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 初始化微信 API
        api = WXAPIFactory.createWXAPI(this, "your_wechat_app_id", true);
        api.registerApp("your_wechat_app_id");

        findViewById(R.id.btn_login_wechat).setOnClickListener(v -> loginWithWeChat());
    }

    private void loginWithWeChat() {
        if (!api.isWXAppInstalled()) {
            Toast.makeText(this, "请先安装微信客户端", Toast.LENGTH_SHORT).show();
            return;
        }

        // 构造登录请求
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo";
        api.sendReq(req);
    }
}

4. 获取 Access Token 和用户信息

通过 WXEntryActivity 获取到的 code,向微信服务器请求 access_token 和用户信息:

请求 Access Token

向以下 URL 发送请求:

https://api.weixin.qq.com/sns/oauth2/access_token?appid=your_appid&secret=your_appsecret&code=CODE&grant_type=authorization_code

获取用户信息

使用 Access Token 调用用户信息接口:

https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID

5. 安全注意事项

  • 不要在客户端存储敏感信息(如 AppSecret)。
  • 在服务器端完成 access_token 和用户信息的管理。
  • 使用 HTTPS 确保数据传输安全。

通过以上步骤,您可以成功实现微信第三方登录并调用登录授权界面。如果需要更详细的解释或具体问题解决,请告诉我!

发布者:myrgd,转载请注明出处:https://www.object-c.cn/5083

Like (0)
Previous 2024年12月3日 下午9:35
Next 2024年12月3日 下午9:59

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

联系我们

在线咨询: QQ交谈

邮件:723923060@qq.com

关注微信