0

最近、ハイブリッド モバイル アプリ (Ionic Framework で構築) に Wechat 認証を使用しようとしましたが、うまくいきませんでした。開発者アカウントにサインアップし、AppId と AppKey を取得しました。その後、リクエストを送信し、ネイティブの Wechat アプリにリダイレクトされましたが、「おっと! 何か問題が発生しました」というエラー メッセージが表示されたページに到達しました。また、次のように Postman 経由で get リクエストを発行しようとしました。

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

しかし、エラーを受け取りました:

{ "errcode": 50001, "errmsg": "ユーザーは許可されていません" }

誰にもアイデアがありますか?ありがとう!

ちなみに、これは私が使用している Wechat プラグインです。

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

4

2 に答える 2

1

Ionicとそのプラグインも使用していますが、問題なく動作します。

オンライン バージョンでは QR コードをスキャンする必要がありますが、iOS または Android では、wechat クライアントが呼び出されます。

あなたが提供したものから、問題は次のように推測されます。

  • Wechat.auth()wechat のユーザー情報を取得する前に電話をかけなかった
  • を使用していませんでした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 に答える