23andMe API错误:'未提供grant_type。''invalid_request' OAuth2

3

我正在使用OAuth 2与23andMe API进行身份验证。在用户授权后,我能够收到代码。我目前正在尝试发送一个post请求以接收访问令牌。但是,我一直收到这个错误:

 data: 
    { error_description: 'No grant_type provided.',
      error: 'invalid_request' } }

我正在使用axios包来进行我的post请求。但是出现了一个错误,因为当我使用cuRL时,我得到了一个成功的200响应和访问令牌:

 curl https://api.23andme.com/token/
      -d client_id='zzz' \
      -d client_secret='zzz' \
      -d grant_type='authorization_code' \
      -d code=zzz \
      -d "redirect_uri=http://localhost:3000/receive_code/"
      -d "scope=basic%20rszzz"

我能够从23andMe服务器接收到授权码,然后被重定向到我的应用程序。这是我的GET路由:

router.get('/receive_code', function(req, res) {

 axios.post('https://api.23andme.com/token/', {

  client_id: zzz,
  client_secret: zzz, 
  grant_type: 'authorization_code',
  code: req.query.code,
  redirect_uri: 'http://localhost:3000/receive_code/',
  scope: "basic%20rs3094315"

  }).then(function (response) {
    console.log(response);

  }).catch(function (error) {
    console.log(error);
 });
});

有什么想法吗?
2个回答

0

问题出在你放置在载荷中的form键上。它应该像这样工作:

axios.post('https://api.23andme.com/token/', {
  client_id: zzz,
  client_secret: zzz, 
  grant_type: 'authorization_code',
  code: req.query.code
  redirect_uri: 'http://localhost:3000/receive_code/',
  scope: "basic%20rs3094315"
}).then(function (response) {
  console.log(response);
}).catch(function (error) {
  console.log(error);
});

@vincentjp,同样的错误吗?看看这个仓库:http://github.com/joelalejandro/genocog-api,我在那里实现了一些连接到23andme的东西。但请注意,他们的演示配置文件存在问题,因此可能某些API调用根本无法工作。 - Joel A. Villarreal Bertoldi

0
我能够通过使用 simple-oauth2 npm 包来解决问题。 它可以在这里找到:https://www.npmjs.com/package/simple-oauth2#express-and-github-example
 // **********23ANDME OAUTH2************
 var oauth2 = require('simple-oauth2')({
  clientID: 'zzz',
  clientSecret: 'zzz',
  site: 'https://api.23andme.com',
  tokenPath: '/token',
  authorizationPath: '/authorize'
 });

 var authorization_uri = oauth2.authCode.authorizeURL({
  redirect_uri: 'http://localhost:3000/receive_code/',
  scope: 'basic  analyses rs1234567',
  state: 'jenvuece2a'
 });
 // *************************************

// In you view, place "/auth" in your <a> e.g. <a href="/auth">Click Me</a>
router.get('/auth', function (req, res) {

 res.redirect(authorization_uri);

});

router.get('/receive_code', function(req, res) {

 var code = req.query.code;

 if (!code) {
  res.send('Error!!')
 } else {
 console.log('running');

  oauth2.authCode.getToken({
    code: code,
    redirect_uri: 'http://localhost:3000/receive_code/'
  }, saveToken);

  function saveToken(error, result) {
    if (error) { 
      console.log('Access Token Error', error.message); 
    } else {
      token = oauth2.accessToken.create(result);
      console.log(token);
    }

  };

  res.render('/genetic_report', {layout: 'dash'});

 }
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接