0

我最近尝试为我的混合移动应用程序(使用 Ionic 框架构建)使用微信身份验证,但没有取得多大成功。我注册了一个开发者账号,获得了一个AppId和AppKey。然后我发出了请求,并被重定向到我的本地微信应用程序,但到达一个页面并显示错误消息“糟糕!出了点问题”。我还尝试通过 Postman 发出 get 请求,例如:

https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=yy8f70a5c5a0971111&secret=99999ed33fe7c954bc672630afb7xxxx

但收到一个错误:

{“errcode”:50001,“errmsg”:“用户未授权”}

有人会有想法吗?谢谢!

顺便说一下,这是我正在使用的微信插件:

https://github.com/xu-li/cordova-plugin-wechat-example

4

2 回答 2

3

这种情况经常发生,因为您没有在您的微信帐户上设置正确的 oauth。

在侧面板点击开发/接口权限开发/接口权限,然后找到账号页面/授权用户获取基本信息页面点击修改网页服务/网页授权获取用户基本信息/点击修改。确保将您用于应用程序的 url 放在 Authorization 回调页面名称/授权页面域名:字段中。

请注意,这不适用于 ip 地址或 localhost,因此您可能需要下载https://ngrok.com/并将代理隧道用作开发的授权回调。

另请注意,微信的身份验证只能在其自己的浏览器中使用。

于 2016-03-08T18:00:51.807 回答
1

我也使用Ionic和那个插件,它们工作正常:

在线版需要扫二维码,而在iOS或安卓中,会调用微信客户端。

根据您提供的信息,我推断问题是:

  • Wechat.auth()您在尝试获取微信用户信息之前没有打电话
  • 您没有使用https://api.weixin.qq.com/sns/oauth2/access_token,而是使用了另一个 url

我在下面附上了我的主要工作代码。最重要的Wechat.auth()在其他员工之前打电话,我在你的代码中没有看到。该Wechat对象由该插件提供,并将执行授权并返回一个代码。使用该代码,我们可以做接下来的事情。

var scope = "snsapi_userinfo";
Wechat.auth(scope, function(response) {
  // Here we got authorized, now we can get more user info by passing the code returned by the authorized response code
  if (response && response.code) {
    OAuthService.weixin.getNativeUserInfoByCode(response.code, wechatSignIn);
  } else {
    // If we don't get the code so we show an alert
    DialogsService.alert('获取微信授权代码失败。' + JSON.stringify(response), '微信授权失败');
  }
}, function(reason) {
  console.error(reason);
  console.error('微信认证失败');
  DialogsService.alert(reason, '微信认证失败');
});

获取用户信息的代码OAuthService.weixin.getNativeUserInfoByCode()如下所示:

$http({
    method: 'POST',
    url: 'https://api.weixin.qq.com/sns/oauth2/access_token?appid={1}&secret={2}&grant_type=authorization_code&redirect_uri={3}&code={the code we got from Wechat.auth()}'
  })
  .success(function(data) {
    if (data.errcode || data.errmsg) {
      console.log('getting weixin access_token fail. data = ');
      console.log(JSON.stringify(data));
      console.log('换取 access_token 的 code 为 "' + code + '"');
      errorCallback(data);
      return;
    }

    console.log('getting weixin access token success. data =');
    console.log(JSON.stringify(data));

    var access_token = data.access_token;
    var openid = data.openid;
    successCallback(access_token, openid);
  })
  .error(function(data) {
    console.log('getting weixin access_token fail. data = ');
    console.log(JSON.stringify(data));

    errorCallback(data);
  });

于 2015-07-30T03:59:37.683 回答