【发布时间】:2019-05-13 23:42:48
【问题描述】:
我尝试使用 Gmail API 和 React 创建应用程序。对于授权,我使用lib 反应谷歌登录
export default class App extends Component {
service = new Service();
render() {
const responseGoogle = (response) => {
this.service.getMessagesList();
}
return (
<div>
<GoogleLogin
clientId="YOUR_ID.apps.googleusercontent.com"
buttonText="Login"
onSuccess={responseGoogle}
/>
授权后,我向 gmail API 发送获取请求并尝试获取我的电子邮件列表。但服务器返回 401。为什么?我做错了什么?
我的收获:
export default class Service {
_apiBase = "https://www.googleapis.com/gmail/v1/users/me";
async getResource(url) {
const res = await fetch(`${this._apiBase}${url}`);
if (!res.ok) {
throw new Error(`Could not fetch ${url}, received ${res.status}`);
}
const body = await res.json();
return body;
}
getMessagesList = async () => {
const list = await this.getResource(`/messages`);
return list;
};
【问题讨论】: