即使使用OFFINE_ACCESS作用域,OneDrive也不返回刷新令牌 [英] onedrive does not return refresh token even with 'offline_access' scope

查看:9
本文介绍了即使使用OFFINE_ACCESS作用域,OneDrive也不返回刷新令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取用户的令牌,以便在我正在构建的应用程序中集成一个驱动器

我首先获得了身份验证URL,下面是端点实现

const pca = new msal.ConfidentialClientApplication(config);
exports.getAuthUrl = (req, res) => {
  const authCodeUrlParameters = {
    scopes: ['user.read', 'files.readwrite', 'offline_access'],

    redirectUri: 'https://localhost:8080/onedrive',
  };
  // get url to sign user in and consent to scopes needed for application
  pca
    .getAuthCodeUrl(authCodeUrlParameters)
    .then((response) => {
      res.status(200).send(response);
    })
    .catch((error) => console.log(JSON.stringify(error)));
};

然后使用客户端身份验证成功后返回的代码作为参数传递到第二个终结点以获取令牌

exports.getToken = (req, res) => {
  const tokenRequest = {
    code: req.query.code,
    scopes: ['user.read', 'files.readwrite', 'offline_access'],

    redirectUri: 'https://localhost:8080/onedrive',
  };

  pca
    .acquireTokenByCode(tokenRequest)
    .then((response) => {
      console.log('
Response: 
:', response);
      res.status(200).send(response);
    })
    .catch((error) => {
      console.log(error);
      res.status(500).send(error);
    });
};
因此,正如官方文档中所提到的,如果添加Offline_Access作用域,您将得到一个刷新令牌

有没有人有这方面的经验?我使用了这两个库,其中部分代码已由Microsoftconst msal = require('@azure/msal-node');

提供

推荐答案

经过几天的研究,我发现msal-node在设计上并不向最终用户公开刷新令牌。当您需要新的访问令牌时,它在内部存储和使用。您应该在每次需要访问令牌时调用quireTokenSilent,msal-node将通过向您返回缓存的令牌或使用刷新令牌来获取新的访问令牌来管理令牌。 所以我所做的实际上是直接调用Microsoft提供的端点 您应该首先使用本文档[https://docs.microsoft.com/en-us/onedrive/developer/rest-api/getting-started/msa-oauth?view=odsp-graph-online][1]提供的URL获取代码 获取代码并将其传递到下一个enpoint,如下所示

exports.getToken = async (req, res) => {
  request.post(
    'https://login.live.com/oauth20_token.srf',
    {
      form: {
        code: req.body.code,
        client_id: 'CLIENT-ID',
        redirect_uri: 'REDIRECT-URI',
        client_secret: 'CLIENT-SECRET',
        grant_type: 'authorization_code',
      },
    },
    async function (err, httpResponse, body) {
      if (err) {
        res.status(500).send({ Message: err.message });
      } else {
        let response = JSON.parse(body);
       res.status(200).send({ Body: JSON.parse(body) });
      }
    }
  );
};

这就是我是如何工作的,使用msal是不可能的

这篇关于即使使用OFFINE_ACCESS作用域,OneDrive也不返回刷新令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆