【发布时间】:2018-05-05 00:06:31
【问题描述】:
作为学习 OAuth2 和使用 react-native 持久登录的一部分,我正在使用找到的 OAuth2 服务器示例 here 来确定如何从服务器发送信息和请求验证。
目前,我希望能够将 access_token 记录到控制台,因为我认为我可以自己建立这些知识。我似乎找不到一个如何正确获取的理想示例。
我认为我没有收到的部分是 header 和 body 信息的发送。
export default class App extends Component {
goGetToken() {
return fetch('http://localhost:3000/oauth/token/?grant_type=password&username=pedroetb&password=password', {
method: 'POST',
headers: {
'Authorization': 'Basic YXBwbGljYXRpb246c2VjcmV0',
'Content-Type': 'application/application/x-www-form-urlencoded',
},
}).then(response => response.json())
.then(responseJson => {
console.log(responseJson.access_token);
return responseJson.access_token;
})
.catch(error => {
console.error(error);
});
}
componentDidMount() {
this.goGetToken();
}
render() {
return(
<View></View>
)
}
}
包含的自述文件建议以下内容,但我似乎找不到正确发送标头/获取的示例?
### Obtaining a token
To obtain a token you should POST to `http://localhost:3000/oauth/token`.
#### With *password* grant
You need to include the client credentials in request headers and the user credentials and grant type in request body:
* **Headers**
* **Authorization**: `"Basic " + clientId:secret base64'd`
* (for example, to use `application:secret`, you should send `Basic YXBwbGljYXRpb246c2VjcmV0`)
* **Content-Type**: `application/x-www-form-urlencoded`
* **Body**
* `grant_type=password&username=pedroetb&password=password`
* (contains 3 parameters: `grant_type`, `username` and `password`)
【问题讨论】:
标签: node.js react-native oauth-2.0 fetch-api