所以我正在建立一个状态板,供我们作为办公室的开发人员在内部使用。它将显示提交的数量、跟踪的小时数等。
我正在遵循此模型进行身份验证。在用户使用 Harvest 登录后,它会将代码参数作为查询字符串重定向回应用程序,然后我将获取该查询字符串并将其传递给一个状态,然后进行获取以获取访问令牌(这样我以后可以提取 API 数据)。
发生的情况是登录成功,但是当您被重定向回应用程序时,提取会引发错误请求 (400) 错误。我已经在 Postman 中进行了测试,并且可以得到正确的响应,所以我不确定问题是什么......
以下是 Main 组件中的一些 JS,如果有代码参数,它们会设置状态:
harvestState() {
// grab URL params
let urlParams = queryString.parse(location.search);
console.log(urlParams);
console.log(urlParams.code);
// set the state based on the paramater passed back
urlParams.code ? (
this.setState({
harvestcode: urlParams.code
})
) : (
this.setState({
harvestcode: 'none'
})
);
}
componentWillMount(){
this.harvestState();
}
这是我的 Harvest 组件中的 fetch 函数:
getHarvest(){
const clientSecret = 'XXXXXXXXXX';
// Set Harvest Headers
const harvestHeaders = {
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'POST',
mode: 'no-cors',
body: {
'code': this.props.code,
'client_id': this.props.clientid,
'client_secret': clientSecret,
'redirect_uri': 'http://dash.mycompany.me',
'grant_type': 'authorization_code'
}
};
fetch('https://mycompany.harvestapp.com/oauth2/token', harvestHeaders)
.then( response => response.json() )
.then( token => {
console.log(token);
} )
}
componentDidMount(){
if( this.props.code !== 'none' ){
this.getHarvest();
}
}
这里有什么我做错了吗?为什么它总是返回一个错误的请求?任何帮助,将不胜感激。谢谢!