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);
});