JeffreySu / WeiXinMPSDK

微信全平台 .NET SDK, Senparc.Weixin for C#,支持 .NET Framework 及 .NET Core、.NET 8.0。已支持微信公众号、小程序、小游戏、微信支付、企业微信/企业号、开放平台、JSSDK、微信周边等全平台。 WeChat SDK for C#.
https://weixin.senparc.com
Apache License 2.0
8.39k stars 4.34k forks source link

BusinessApi.GetUserPhoneNumberAsync(appId, code); 调用失败 #2558

Open NeverMoreJhon opened 2 years ago

NeverMoreJhon commented 2 years ago

环境:.netcore 3.1

BusinessApi.GetUserPhoneNumberAsync(appId, code); 调用失败
重现问题步骤(如果可以)
  1. 调用 AccessTokenContainer.RegisterAsync(appId,appSecreat)注册,这里使用的是微信小程序的appId
  2. 调用 BusinessApi.GetUserPhoneNumberAsync(appId, code)
  3. 返回 {"phone_info":null,"errcode":40013,"ErrorCodeValue":40013,"errmsg":"invalid appid rid: 61ef911d-660e539f-1c6f321e","P2PData":null}
  4. 如果第二步注册的是微信开放平台的appId, 则返回{"phone_info":null,"errcode":61024,"ErrorCodeValue":61024,"errmsg":"must use api(api_component_token) to get token for component acct rid: 61ef5ea0-6948b455-30d85954","P2PData":null}

Email:573253296@qq.com

JeffreySu commented 2 years ago

麻烦把涉及上述1,2步骤的代码直接复制上来一下。

NeverMoreJhon commented 2 years ago

这是来自QQ邮箱的假期自动回复邮件。您好,我最近正在休假中,无法亲自回复您的邮件。我将在假期结束后,尽快给您回复。

NeverMoreJhon commented 2 years ago

第一步:在服务启动时注册  AccessTokenContainer.RegisterAsync(小程序appId, 小程序appsecret);

第二步:根据UI传过来的Code 调用

现在问题主要是搞不清  在调用BusinessApi.GetUserPhoneNumberAsync和注册 AccessTokenContainer.RegisterAsync时到底传微信小程序的appid 还是传  微信第三方的appId,

------------------ 原始邮件 ------------------ 发件人: "JeffreySu/WeiXinMPSDK" @.>; 发送时间: 2022年1月27日(星期四) 中午11:24 @.>; 抄送: "Fish @.**@.>; 主题: Re: [JeffreySu/WeiXinMPSDK] BusinessApi.GetUserPhoneNumberAsync(appId, code); 调用失败 (Issue #2558)

麻烦把涉及上述1,2步骤的代码直接复制上来一下。

— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.Message ID: @.***>

JeffreySu commented 2 years ago

您好,需要直接复制完整的代码片段(如果有敏感的字符串请替换掉),否则无法具体帮你判断,比较怀疑是调用方法或者顺序不正确。

譬如:AccessTokenContainer.RegisterAsync(小程序appId, 小程序appsecret);

如果您只是这样执行注册过程是不对的,这是一个异步方法,如果不等待的话,会直接向下继续执行,当你执行到下面的代码时,注册过程可能并没有完成。

NeverMoreJhon commented 2 years ago

好的,上面是在邮件里面回复的,导致图片没有加载成功。下面我详细说一下: 环境,.net Core 3.1,输出类型为控制台应用程序; 在program.cs 里注册: public class Program { public static void Main(string[] args) { ServiceSetup.RegisterMsspService(options => {

            options.Configure = (app,env) =>
            {
                var senparcSetting = new SenparcSetting()
                {
                    IsDebug = true,
                    Cache_Memcached_Configuration = "",
                    Cache_Redis_Configuration = "",
                    DefaultCacheNamespace = "DefaultCache",
                    SenparcUnionAgentKey = ""
                };
                var config = SystemPrivateConfig.Instance.GetConfigByAppId();
                var weixinSetting = new SenparcWeixinSetting()
                {
                    Component_Appid = config.AppId,
                    Component_Secret = config.AppSecret,
                    Component_EncodingAESKey = config.EncodingAESKey,
                    Component_Token = config.Token
                };
                app.UseSenparcGlobal(env, senparcSetting, reg =>
                {

                },true).UseSenparcWeixin(weixinSetting, reg =>
                {
                    reg.RegisterOpenComponent(weixinSetting, GetTicket, GetRefreshToken, RefreshToken, "开放平台");
                });
                **AccessTokenContainer.RegisterAsync(appid,appSecret);**
                app.UseAuthorization();
            };

        });
    }

疑问:AccessTokenContainer.RegisterAsync注册时,使用哪个模块注册(有mp和wxOpen两个模块)?注册哪个appId和appSecret?

调用时:
LogHelper.Info($"appId:{appId},code:{encryptedData},openAccessToken:{openAccessToken}"); var phoneInfo = await BusinessApi.GetUserPhoneNumberAsync(openAccessToken, encryptedData); LogHelper.Info($"新方式获取手机号:{phoneInfo.ToJson()}");

jerronwong commented 2 years ago

邮件已收到,我会尽快回复您,谢谢!  

JeffreySu commented 2 years ago

@NeverMoreJhon

AccessTokenContainer.RegisterAsync注册时,使用哪个模块注册(有mp和wxOpen两个模块)?注册哪个appId和appSecret?

答:你用哪个命名空间下的 AccessTokenContainer,就是哪个,如果你两个都 using 了,需要输入命名空间加以区分,否则就是你 using 的那个。注册的 appId 和 secret 就是对应的 MP 或者 WxOpen 的。

比较有可能的问题出在你的注册代码:

AccessTokenContainer.RegisterAsync(appid,appSecret);

这个是异步方法,你需要这样做:

 AccessTokenContainer.RegisterAsync(appid,appSecret).GetWaiter().GetResult();

或者直接使用同步方法。

NeverMoreJhon commented 2 years ago

已经解决了,感谢指点