如何在没有用户交互的情况下以自动方式访问 onedrive?

How do I access onedrive in an automated fashion without user interaction?

我正在尝试通过 onedrive 的 api 访问我自己的文档和电子表格。我有:

import requests

client_id = 'my_id'
client_secret = 'my_secret'
scopes = 'wl.offline_access%20wl.signin%20wl.basic'
response_type = 'token' # also have tried "code"
redirect_uri = 'https://login.live.com/oauth20_desktop.srf'

base_url = 'https://apis.live.net/v5.0/'

r = requests.get('https://login.live.com/oauth20_authorize.srf?client_id=%s&scope=%s&response_type=%s&redirect_uri=%s' % (client_id, scopes, response_type, redirect_uri))
print r.text

(对于我的客户,我也尝试将 "Mobile or desktop client app:" 设置为 "Yes" 和 "No")

这将return html 供用户手动点击。由于用户是我,这是我的帐户,我如何在没有用户交互的情况下访问 API?

编辑#1: 对于那些对我正在寻找的内容感到困惑的人来说,它相当于 Google 的服务帐户 (OAuth2):https://console.developers.google.com/project

您不能"bypass" 用户交互。

但是你已经非常接近让它工作了。如果你想在 python 中获得访问令牌,你必须通过浏览器来完成。您可以使用 Web 浏览器库打开默认的 Web 浏览器。它看起来像这样(您的应用程序必须是桌面应用程序):

import webbrowser    
webbrowser.open("https://login.live.com/oauth20_authorize.srf?client_id=foo&scope=bar&response_type=code&redirect_uri=https://login.live.com/oauth20_desktop.srf")

这会将您带到授权页面,登录并同意条款(视范围而定)。它会将您定向到 url 看起来像这样的页面:

https://login.live.com/oauth20_desktop.srf?code=<THISISTHECODEYOUWANT>&lc=foo

从浏览器复制此代码,让您的 python 脚本将其作为输入。

然后,您可以使用从浏览器收到的代码,按照描述here提出请求。

您将收到描述的回复 here