# 开通

  • 登录新浪微博开放平台,打开 “移动应用 MOBILE” 页面
  • 在页面中选择 “立即接入”,根据提示填写信息创建应用
  • 创建成功后,在 “我的应用” 中点击应用,可以在应用详情页面
  • 在应用详情页面的 “应用信息” -> “基本信息” 中可获取 App Key,点击编辑可设置iOS平台的通用链接(UniversalLink)
  • 在应用详情页面的 “应用信息” -> “高级信息” -> “OAuth2.0 授权设置” 中可以设置授权回调页(redirect_url)

For more information, please refer to the official Sina Weibo document Mobile Application Access

# 配置

  • appkey 新浪微博开放平台申请应用的AppKey值
  • redirect_url 新浪微博开放平台申请应用中设置的授权回调页
  • UniversalLinks iOS平台通用链接,必须与新浪微博开放平台配置的一致,推荐使用一键生成iOS通用链接

Notice

  • The standard real machine running base in HBuilderX uses the AppID and other information that DCloud applies for the HBuilder application, which is only used to experience the Sina Weibo login function
  • The configuration parameters need to be submitted to the cloud to be packaged to take effect. Please use the [custom debugging base] when the real machine is running (https://ask.dcloud.net.cn/article/35115)

# 使用新浪微博登录

# 示例代码

  • uni-app项目
uni.login({
    provider: 'sinaweibo',
    success: function (loginRes) {
        // login successful
        uni.getUserInfo({
            provider: 'sinaweibo',
            success: function(info) {
                // Obtain user information successfully, info.authResult saves user information
            }
        })
    },
    fail: function (err) {
        // 登录授权失败
        // err.code is the error code
    }
});
  • 5+ App项目
var weiboOauth = null;
plus.oauth.getServices(function(services) {
	for (var i in services) {
		var service = services[i];
		// 获取新浪微博登录对象
		if (service.id == 'sinaweibo') {
			weiboOauth = service;
			break;
		}
	}
	weiboOauth.login( function(oauth){
		// 授权成功,weiboOauth.authResult 中保存授权信息
	}, function(err) {
    // 登录授权失败
    // err.code is the error code
	})
}, function(err) {
	// Failed to get services
})