老E的博客

  • 首页
  • 关于
  • 技术应用
    • VPS相关
    • AI相关
    • 盒子相关
    • 其他
  • 订阅Youtube频道
  • 网络加速
    • expressvpnNo.1 ExpressVPN
    • 最佳免费VPN-PrivadoVPN
    • NordVPN
    • 廉价王者-Surfshark
    • PrivateInternetAccess
    • 解锁一切-PrivateVPN
  • 公益资源
    • AI导航
    • 工具下载
    • Docker镜像加速
  • 友情链接
    • tickcloudTickcloud
老E的博客
专注记录并分享跨境技术应用及随想
  1. 首页
  2. 技术应用
  3. 正文

Onedrive应用程序注册及获取令牌

2023年8月6日 1588次阅读 0条评论
clawcloud
expressvpn best vpn
privado vpn
surfshark vpn
private vpn
pia vpn
nord vpn

网盘列表程序通过API访问Onedrive,需要在Azure AD管理中完成注册应用程序、设置Auth重定向、创建API访问密钥、授权等几个步骤的工作。以下按步骤详细列出。

登录Azure Portal,点击“管理Azure Active Directory”打开AD管理页面(或者搜索栏搜索Acative Directory后打开)。

在Azure活动目录管理概述页,选择“应用注册”,打开应用注册页。

应用注册页面,点击“新注册”以开始注册新应用程序。

分别输入应用程序名称,在“重定向URI(可选)”选择重定向URI类型为web,URI可设为onedrive列表程序(网盘程序)指定的内容或者简易设为"http://localhost"即可。

完成注册后,在概述页面选择“证书与密钥”。

选择"+新客户端密码",开始创建密钥。

 

右侧会弹出添加客户端密码选项卡,输入密码说明,选择密码有效期后,在左下角点击创建。

创建完成后,复制新创建的密码(表格中“值”列),该密码(secret)仅在创建完成时完整显示一次。

回到应用程序概述页面,选择“API权限”对应用程序授权,之后选择“添加权限”。

在弹出的请求获取API权限选项卡,点选“Microsoft Grapg”面板。

之后,选择“委托的权限”,搜索“Files”(区分大小写),将所列3项勾选后,单击“添加权限”按钮。这里仅为Read权限,可结合实际需要选择,个人网盘进列表、直链下载无需Write,可于官方onedrive或sharepoint管理页面对网盘文件进行上传、管理。

系统部署后,回到权限概述页面,会列出刚添加的三项API权限(User read为应用程序创建时AD默认添加)。

回到应用程序概述页,可以将应用程序ID(Client ID)拷贝备用。

同时,URI重定向可在身份验证页面进行添加、修改,或者结合列表程序的安装部署、使用情况进行调整。

至此,应用程序、密码、授权均已配置完成,可按照列表程序部署要求(如alist获取刷新令牌)进行下一步,或者参考如下PHP代码自行获取并保管好自己的访问令牌(Access Token)。

<?php
$clientID = 'CLIENT_ID';
$clientSecret = 'SECRET';
$tokenURL = 'https://login.microsoftonline.com/TENANT_ID/oauth2/token';
$data = array(
    'grant_type' => 'client_credentials',
    'client_id' => $clientID,
    'client_secret' => $clientSecret,
    'resource' => 'https://graph.microsoft.com' 
);
$options = array(
    'http' => array(
        'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
        'method'  => 'POST',
        'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($tokenURL, false, $context);
$tokenData = json_decode($result, true);
if (isset($tokenData['access_token'])) {
    $accessToken = $tokenData['access_token'];
    $expiresIn = $tokenData['expires_in'];

    $expirationDateTime = new DateTime();
    $expirationDateTime->add(new DateInterval('PT' . $expiresIn . 'S'));
    $expirationDate = $expirationDateTime->format('Y-m-d H:i:s');

    echo "获取到的AccessToken: " . $accessToken . "<br>";
    echo "AccessToken的过期时间: " . $expirationDate . "<br>";
} else {
    echo "无法获取AccessToken";
}
?>

 

本作品采用 知识共享署名-非商业性使用-禁止演绎 4.0 国际许可协议 进行许可
标签: onedrive
最后更新:2024年5月28日

老E

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

文章评论

razz evil exclaim smile redface biggrin eek confused idea lol mad twisted rolleyes wink cool arrow neutral cry mrgreen drooling persevering
取消回复

站内搜索
归档
  • 2025 年 5 月
  • 2025 年 4 月
  • 2025 年 3 月
  • 2025 年 2 月
  • 2025 年 1 月
  • 2024 年 12 月
  • 2024 年 11 月
  • 2024 年 10 月
  • 2024 年 9 月
  • 2024 年 8 月
  • 2024 年 7 月
  • 2024 年 6 月
  • 2024 年 5 月
  • 2024 年 4 月
  • 2024 年 3 月
  • 2024 年 2 月
  • 2024 年 1 月
  • 2023 年 12 月
  • 2023 年 11 月
  • 2023 年 10 月
  • 2023 年 9 月
  • 2023 年 8 月
  • 2023 年 7 月
  • 2023 年 6 月

Copyright ©2023-2025 Appscross. All Rights Reserved.