主要能用于获取用户的userId,能用于创建交易场景、发送小程序模板消息场景使用。
方法定义
该方法的定义如下:
oauth: {
getToken(params: GetTokenRequest): Promise<FunctionResponse<GetTokenResponse>>
};
请求参数
该方法接收以下请求参数。
字段名 | 类型 | 必填 | 说明 |
---|---|---|---|
grantType | string | 是 | 授权类型,小程序端使用authorization_code。 |
code | String | 是 | 用户授权码。 |
返回参数
字段名 | 类型 | 是否必有 | 说明 |
---|---|---|---|
userId | string | 是 | 用户 ID。 |
accessToken | string | 是 | 授权返回的访问 token 令牌,一般情况不需要。 |
示例
getToken() {
my.getAuthCode({
scopes: 'auth_user',
success: async authRes => {
const { authCode } = authRes;
if(authCode) {
try {
const res = await cloud.base.oauth.getToken({
grantType: 'authorization_code',
code: authCode,
});
const { userId } = res;
// 做其他的逻辑
} catch (e) {
// 处理异常逻辑
}
}
}
});
}