开发者社区 > 数据库 > NoSQL数据库 > 正文

我用了官方库 alibabacloud-go/dingtalk 应用开了权限,接口还是报403

我在使用官方的库 go sdk github.com/alibabacloud-go/dingtalk 对接 钉钉接口。

我开通了一个应用,也开通了权限。通过官方的sdk调用还是报错了。说的是我没有权限,我的应用是开通了权限的。

报错信息,

code: 403, 没有调用该接口的权限,接口权限申请参考:https://open.dingtalk.com/document/orgapp-server/add-api-permission request id: B4DB99DC-706A-767D-BBDA-EF61D94C89E6
{"accessdenieddetail":{"requiredScopes":["Contact.User.Read"]},"code":"Forbidden.AccessDenied.AccessTokenPermissionDenied","message":"没有调用该接口的权限,接口权限申请参考:https://open.dingtalk.com/document/orgapp-server/add-api-permission","requestid":"B4DB99DC-706A-767D-BBDA-EF61D94C89E6","statusCode":403}

我的应用:
企业微信20240525-152257.png

权限是开了的,也发布了。

代码:


import (
    openapi "github.com/alibabacloud-go/darabonba-openapi/v2/client"
    dingtalkcontact_1_0 "github.com/alibabacloud-go/dingtalk/contact_1_0"
    dingtalkoauth2_1_0 "github.com/alibabacloud-go/dingtalk/oauth2_1_0"
    util "github.com/alibabacloud-go/tea-utils/v2/service"
    "github.com/alibabacloud-go/tea/tea"
)

const (
    CLIENT_ID     = "dingd"
    CLIENT_SECRET = "_4hRhSzdL"
)

func CreateClient() (_result *dingtalkcontact_1_0.Client, _err error) {
    config := &openapi.Config{}
    config.Protocol = tea.String("https")
    config.RegionId = tea.String("central")
    _result = &dingtalkcontact_1_0.Client{}
    _result, _err = dingtalkcontact_1_0.NewClient(config)
    return _result, _err
}

func CreateClientV2() (_result *dingtalkoauth2_1_0.Client, _err error) {
    config := &openapi.Config{}
    config.Protocol = tea.String("https")
    config.RegionId = tea.String("central")
    _result = &dingtalkoauth2_1_0.Client{}
    _result, _err = dingtalkoauth2_1_0.NewClient(config)
    return _result, _err
}


// GetUserByCode 使用 Token 初始化账号Client
func GetUserByCode(client *dingtalkoauth2_1_0.Client, code string) (re *dingtalkoauth2_1_0.GetUserTokenResponse, _err error) {
    getUserTokenRequest := &dingtalkoauth2_1_0.GetUserTokenRequest{
        ClientId:     tea.String(CLIENT_ID),
        ClientSecret: tea.String(CLIENT_SECRET),
        Code:         tea.String(code),
        GrantType:    tea.String("authorization_code"),
    }
    tryErr := func() (_e error) {
        defer func() {
            if r := tea.Recover(recover()); r != nil {
                _e = r
            }
        }()
        re, _err = client.GetUserToken(getUserTokenRequest)
        if _err != nil {
            return _err
        }

        return nil
    }()

    if tryErr != nil {
        var err = &tea.SDKError{}
        if _t, ok := tryErr.(*tea.SDKError); ok {
            err = _t
        } else {
            err.Message = tea.String(tryErr.Error())
        }
        if !tea.BoolValue(util.Empty(err.Code)) && !tea.BoolValue(util.Empty(err.Message)) {
            // err 中含有 code 和 message 属性,可帮助开发定位问题
        }
    } else if re != nil {
        logger.Info("get user token success: %+v", *re)
    }
    return re, _err
}

func GetUserInfoByAccessToken(client *dingtalkcontact_1_0.Client, accessToken, unionId string) (re *dingtalkcontact_1_0.GetUserResponse, _err error) {
    getUserHeaders := &dingtalkcontact_1_0.GetUserHeaders{}
    getUserHeaders.XAcsDingtalkAccessToken = tea.String(accessToken)
    tryErr := func() (_e error) {
        defer func() {
            if r := tea.Recover(recover()); r != nil {
                _e = r
            }
        }()
        re, _err = client.GetUserWithOptions(tea.String(unionId), getUserHeaders, &util.RuntimeOptions{})
        if _err != nil {
            return _err
        }

        return nil
    }()

    if tryErr != nil {
        var err = &tea.SDKError{}
        if _t, ok := tryErr.(*tea.SDKError); ok {
            err = _t
        } else {
            err.Message = tea.String(tryErr.Error())
        }
        if !tea.BoolValue(util.Empty(err.Code)) && !tea.BoolValue(util.Empty(err.Message)) {
            // err 中含有 code 和 message 属性,可帮助开发定位问题
        }

    }
    return re, _err
}


// TestUserLoginGetInfo 扫码验证code获取 access token,再通过 access token 获取用户信息
func TestUserLoginGetInfo(t *testing.T) {
    var code = "827269eec875339db1f75c227f5caa86"
    client2, err := CreateClientV2()
    if err != nil {
        t.Errorf("CreateClientV2() error = %v", err)
    }
    if re, err := GetUserByCode(client2, code); err != nil {
        t.Errorf("GetUserByCode() error = %v ", err)
    } else if re != nil {
        t.Logf("GetUserByCode() re = %+v", *re)
        client, err := CreateClient()
        if err != nil {
            t.Errorf("CreateClient() error = %v", err)
            return
        }
        gotRe, err := GetUserInfoByAccessToken(client, *re.Body.AccessToken, "me")
        if err != nil {
            t.Errorf("GetUserInfoByAccessToken() error = %v", err)
            return
        } else if gotRe != nil {
            t.Logf("GetUserInfoByAccessToken() gotRe = %+v", *gotRe)
        }
    }
}

所以,问题在哪呢?

展开
收起
为活 2024-05-25 15:38:36 227 0
0 条回答
写回答
取消 提交回答

阿里云NoSQL数据库提供了一种灵活的数据存储方式,可以支持各种数据模型,包括文档型、图型、列型和键值型。此外,它还提供了一种分布式的数据处理方式,可以支持高可用性和容灾备份。包含Redis社区版和Tair、多模数据库 Lindorm、MongoDB 版。

相关电子书

更多
Go语言路上踩过的坑 立即下载
gohbase :HBase go客户端 立即下载
Go构建日请求千亿级微服务实践 立即下载