【问题标题】:Gmail API with React带有 React 的 Gmail API
【发布时间】: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;
};

【问题讨论】:

    标签: reactjs api gmail-api


    【解决方案1】:

    错误 401 表示凭据无效。如果您收到此错误,则很可能是您使用的凭据无效。您可能使用了错误类型的凭据。

    尝试转到console 并再次检查您的凭据。如果您正在寻找有关实施授权的更多信息,您可以参考此documentation

    【讨论】:

      猜你喜欢
      • 2016-02-28
      • 2016-12-19
      • 2023-03-31
      • 1970-01-01
      • 2020-08-24
      • 2015-03-04
      • 2017-05-23
      • 2015-11-28
      • 2020-08-09
      相关资源
      最近更新 更多