【问题标题】:23andMe API error: 'No grant_type provided.' 'invalid_request' OAuth223andMe API 错误:“未提供 grant_type。” 'invalid_request' OAuth2
【发布时间】:2023-03-09 01:25:02
【问题描述】:

我正在使用带有 OAuth 2 的 23andMe API 进行身份验证。我能够在用户授予访问权限后接收代码。我目前正在尝试发送发布请求以接收访问令牌。我继续收到此错误:

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

我正在使用 axios 包来发出我的帖子请求。我的请求中有错误,因为我在 curRL 时获得了成功的 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);
 });
});

有什么想法吗?

【问题讨论】:

    标签: node.js express oauth-2.0 axios 23andme-api


    【解决方案1】:

    问题在于您放置在有效负载中的form 密钥。它应该像这样工作:

    axios.post('https://api.23andme.com/token/', { client_id: zzz, client_secret: zzz, grant_type: '授权码', 代码:req.query.code redirect_uri: 'http://localhost:3000/receive_code/', 范围:“基本%20rs3094315” }).then(函数(响应){ 控制台日志(响应); }).catch(函数(错误){ 控制台日志(错误); });

    【讨论】:

    • 同样的错误@vincentjp?看看this repo,我已经在那里实现了一些连接到 23andme 的东西。但是请注意,他们在演示配置文件中遇到问题,因此可能某些 API 调用可能根本不起作用。
    【解决方案2】:

    我能够通过使用 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'});
    
     }
    });
    

    【讨论】:

      猜你喜欢
      • 2018-01-10
      • 1970-01-01
      • 1970-01-01
      • 2020-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 2014-01-08
      相关资源
      最近更新 更多