Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

为啥我总是save token, 从未get token; #8

Closed
hezedu opened this issue Jul 7, 2015 · 8 comments
Closed

为啥我总是save token, 从未get token; #8

hezedu opened this issue Jul 7, 2015 · 8 comments

Comments

@hezedu
Copy link

hezedu commented Jul 7, 2015

var oauthApi = new OAuth('appid', 'secret', function (openid, callback) {
  // 传入一个根据openid获取对应的全局token的方法

   //从未执行

}, function (openid, token, callback) {
  // 请将token存储到全局,跨进程、跨机器级别的全局,比如写到数据库、redis等
  // 这样才能在cluster模式及多机情况下使用,以下为写入到文件的示例
  // 持久化时请注意,每个openid都对应一个唯一的token!
  fs.writeFile(openid + ':access_token.txt', JSON.stringify(token), callback);
});
@JacksonTian
Copy link
Member

因为你从来不保存。

@hezedu
Copy link
Author

hezedu commented Jul 7, 2015

存mongodb里了,_id是openid。
image

@hezedu
Copy link
Author

hezedu commented Jul 7, 2015

全部代码如下(用的mongoose):

var wechat_oauth_tokens = M_mongo.wechat_oauth_tokens;
var oauth = new OAuth(conf.appid, conf.appsecret,
  function(openid, callback) { //get token
    console.log('get oauthToken 从未执行');
    wechat_oauth_tokens.findOne({
      _id: openid
    }, function(err, doc) {
      if (err) {
        console.error('wechat_oauth_tokens findOne err:');
        console.error(err);
        return callback(err);
      }
      callback(null, doc.token);
    });
  },
  function(openid, tk, callback) { //save token

    wechat_oauth_tokens.findOne({
      _id: openid
    }, function(err, doc) {
      if (err) {
        console.error('wechat_oauth_tokens update err:');
        console.error(err);
        return callback(err);
      }
      if (doc) {//如果有 更新
        doc.token = tk;
        doc.save();
        callback(null);
      } else {
        //如果没有 插入新的
        wechat_oauth_tokens.create({
          _id: openid,
          token: tk
        }, function(err) {
          if (err) {
            console.error('wechat_oauth_tokens create err:');
            console.error(err);
          }
          callback(err);
        });
      }
    });
  });

@JacksonTian
Copy link
Member

mongodb的_id主键不是这样用吧。

在 2015年7月7日,上午10:57,hezedu notifications@github.com 写道:

全部代码如下:

var wechat_oauth_tokens = M_mongo.wechat_oauth_tokens;
var oauth = new OAuth(conf.appid, conf.appsecret,
function(openid, callback) { //get token
console.log('get oauthToken 从未执行');
wechat_oauth_tokens.findOne({
_id: openid
}, function(err, doc) {
if (err) {
console.error('wechat_oauth_tokens findOne err:');
console.error(err);
return callback(err);
}
callback(null, doc.token);
});
},
function(openid, tk, callback) { //save token

wechat_oauth_tokens.findOne({
  _id: openid
}, function(err, doc) {
  if (err) {
    console.error('wechat_oauth_tokens update err:');
    console.error(err);
    return callback(err);
  }
  if (doc) {//如果有 更新
    doc.token = tk;
    doc.save();
    callback(null);
  } else {
    //如果没有 插入新的
    wechat_oauth_tokens.create({
      _id: openid,
      token: tk
    }, function(err) {
      if (err) {
        console.error('wechat_oauth_tokens create err:');
        console.error(err);
      }
      callback(err);
    });
  }
});

});

Reply to this email directly or view it on GitHub #8 (comment).

@nick-ma
Copy link
Member

nick-ma commented Jul 7, 2015

create的时候指定了_id,但还不是标准的ObjectID,没报错已经很给面子了。
当api实例没有token的时候,就回去获取一个新的,然后存进去。

@hezedu
Copy link
Author

hezedu commented Jul 8, 2015

虽然替换掉_id主键不好,但跟_id跟这问题没关系吧。我demo里的文件存储试了下,也是从来不get token。我也不知道什么时候触发get, 我主要是用在下面方法:

client.getAccessToken('code', function (err, result) {
  var accessToken = result.data.access_token;
  var openid = result.data.openid;
});

其中每次code 都不一样。

@darkhe
Copy link

darkhe commented Sep 16, 2015

我也碰到了同样的总是,有大大能指导一下吗

@JacksonTian
Copy link
Member

只有getUser的时候才有读。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants