我是Microsoft Graph API的新手,我正尝试通过NodeJS API访问一个驱动器。我已完成以下操作,从Graph API获取访问令牌,以便访问我的一个驱动器,而无需用户每次要访问我的驱动器上的内容时都必须登录。
const postData = {
client_id: 'xxxxxxxxxxxxxxxxxxx',
scope: 'https://graph.microsoft.com/.default',
client_secret: 'xxxxxxxxxxxxxxxxxxx',
grant_type: 'client_credentials'
};
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
// Get access token and assign it to the token variable
axios
.post('https://login.microsoftonline.com/xxxxxxxxxxxxxxxxxxx/oauth2/v2.0/token', qs.stringify(postData))
.then(response => {
token = response.data.access_token;
}).catch(error => {
console.log(error);
});
我的API已成功获取令牌。但是,当我尝试使用此令牌和文档中提供的端点来访问我的One Drive时遇到错误。代码如下:
const AuthStr = 'Bearer ' + token;
axios.get('https://graph.microsoft.com/v1.0/drive/root/children', { headers: { Authorization: AuthStr } }).then(response => {
console.log(response);
}).catch((error) => {
console.log(error);
});
我在做什么错了?
答案 0 :(得分:1)
您只需要使用以下网址:
https://graph.microsoft.com/v1.0/users/{userid}/drive/root/children
在url中指定用户ID,因为client_credentials授予流的访问令牌不包含用户身份。