首页 > 解决方案 > 如何使用 VueJS axios-oauth-client 和 google photos api?

问题描述

我正在使用 vue cli,并尝试从我的谷歌照片帐户中获取专辑列表。

我正在使用 axios-oauth-client 并尝试实现此代码:

const axios = require('axios');
const oauth = require('axios-oauth-client');
const getAuthorizationCode = oauth.client(axios.create(), {
  url: 'https://oauth.com/2.0/token',
  grant_type: 'authorization_code',
  client_id: 'foo',
  client_secret: 'bar',
  redirect_uri: '...',
  code: '...',
  scope: 'baz',
});

const auth = await getAuthorizationCode(); // => { "access_token": "...", "expires_in": 900, ... }

如此处所述,我不明白如何在此实现中获取授权代码,也就是代码

我设法在邮递员中进行了此调用,但无法使用 axios 进行此调用。

邮差 在此处输入图像描述

我的代码

async function getToken() {
  const getAuthorizationCode = oauth.client(axios.create(), {
    url: "https://oauth2.googleapis.com/token",
    grant_type: "authorization_code",
    client_id:
      "**********.apps.googleusercontent.com",
    client_secret: "***********",
    redirect_uri: "http://localhost:8080/oauth2/callback",
    code: "...",
    scope: "https://www.googleapis.com/auth/photoslibrary.readonly"
  });

  const auth = await getAuthorizationCode(); // => { "access_token": "...", "expires_in": 900, ... }

  console.log(auth);
}

getToken();

我究竟做错了什么?

谢谢!

更新

我仍然没有设法让它工作,但我找到了这个答案,我会尝试检查。

更新#2

最终,我最终使用 php 使用了 Oauth2 的 google文档,并将他们的 git 示例作为基础项目。为了做到这一点,我还需要使用数据库来保存令牌,尤其是每次收到 401 时我用来刷新访问令牌的刷新令牌。

标签: vuejs2axiosvue-cli-3

解决方案


推荐阅读