2016-08-05 70 views
3

我使用OAuth 2的23andMe API進行身份驗證。我可以在用戶授予訪問權限後接收代碼。我目前正在嘗試發送郵件請求以接收訪問令牌。我繼續收到此錯誤:23andMe API錯誤:'沒有提供grant_type。' 'invalid_request'OAuth2

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

我正在使用axios包來提出我的發佈請求。有一個在我的請求的錯誤,因爲我得到了一個成功的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); 
}); 
}); 

有什麼想法?

回答

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

我提出建議的更改。錯誤仍在繼續。 – vincentjp

+0

同樣的錯誤@vincentjp?看看[這個回購](http://github.com/joelalejandro/genocog-api),我已經實現了一些東西連接到23andme。不過,建議他們在演示配置文件中遇到問題,所以也許一些API調用可能根本不起作用。 –

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

} 
}); 
相關問題