我应该选择哪种类型的应用程序/身份验证流程来使用Python脚本和个人Microsoft帐户读取云OneNote内容? [英] What type of app/authentication flow should I select to read my cloud OneNote content using a Python script and a personal Microsoft account?

查看:80
本文介绍了我应该选择哪种类型的应用程序/身份验证流程来使用Python脚本和个人Microsoft帐户读取云OneNote内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我完全不熟悉MS Identity服务,我对需要做的事情有很多选择不知所措

I am completely new to MS Identity services and I am overwhelmed by the many options I have for what I need to do

这是我要达到的目标:我有一个OneNote个人帐户和存储在MS Cloud(我猜是OneDrive)中的笔记.我需要能够运行Python脚本,获取笔记内容,进行一些处理并将其保存回去.这将是Windows10家用计算机上的命令行

Here is what I am trying to achieve: I have a OneNote personal account and notes stored in the MS Cloud (OneDrive I guess). I need to be able to run a Python script, get the content of my notes, do some processing and save them back. This will be from the command line on a home Windows10 computer

我的问题:我应该在MS AD中注册哪种类型的应用程序,以及我应该用于哪种类型的身份验证流程?

My question: what type of application should I register in MS AD and what type of authentication flow should I used for the above?

我尝试了很多事情,这是我所能得到的:
-我在Azure AD中注册了一个应用程序(尝试了个人应用程序和AD应用程序)
-我将该应用程序配置为Windows应用程序-我选择了设备身份验证流程

I have tried many things and this is as far as I could get:
-I registered an app with Azure AD (tried both personal and AD app)
-I configured the app as Windows App -I selected a device authentication flow

我在两种类型的应用程序中都尝试过此代码

I tried this code with both types of app

import requests
import json
from msal import PublicClientApplication
tenant = "5fae6798-ca1a-49d4-a5fb-xxxxxxx" ◄ regular app
client_id = "d03a79d3-1de0-494c-8eb0-xxx"  ◄ personal app
#client_id="bbd3d6df-f5f3-4206-8bd5-xxxxxx"

scopes=["Notes.ReadWrite.All","Notes.Read.All","Notes.Read","Notes.Create","Notes.ReadWrite",
        "Notes.ReadWrite.CreatedByApp","Notes.Read","Notes.Create","Notes.ReadWrite",
        "Notes.ReadWrite.CreatedByApp","Notes.Read.All","Notes.ReadWrite.All"]

endpoint= "https://graph.microsoft.com/v1.0/me"
authority = "https://login.microsoftonline.com/" + tenant

app=PublicClientApplication(client_id=client_id, authority=authority)
flow = app.initiate_device_flow(scopes=scopes)
if "user_code" not in flow:
        raise ValueError(
            "Fail to create device flow. Err: %s" % json.dumps(flow, indent=4))
print(flow["message"])

result = app.acquire_token_by_device_flow(flow)


endpoint= "https://graph.microsoft.com/v1.0/users/c5af8759-4785-4abf-9434-xxxx/onenote/notebooks"
if "access_token" in result:
    # Calling graph using the access token
    graph_data = requests.get(  # Use token to call downstream service
        endpoint,
        headers={'Authorization': 'Bearer ' + result['access_token']},).json()
    print("Graph API call result: %s" % json.dumps(graph_data, indent=2))
else:
    print(result.get("error"))
    print(result.get("error_description"))
    print(result.get("correlation_id"))  # You may need this when reporting a bug

常规应用

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code AH2UHFDXB to authenticate.

Graph API call result: {
  "error": {
    "code": "30108",
    "message": "OneDrive for Business for this user account cannot be retrieved.",
    "innerError": {
      "request-id": "016910d2-c193-4e3f-9d51-52fce86bfc72",
      "date": "2020-05-14T16:45:44"
    }
  }
}

个人应用程序输出

Fail to create device flow. Err: {
    "error": "invalid_request",
    "error_description": "AADSTS9002331: Application 'bbd3d6df-f5f3-4206-8bd5-xxxxxxx'(OneNotePersonal) is configured for use by Microsoft Account users only. Please use the /consumers endpoint to serve this request.\r\nTrace ID: 1c4047e6-98a8-4615-9a0c-4b0dc9ba5600\r\nCorrelation ID: a6733520-6df9-422a-a6b4-e8f4e2de1265\r\nTimestamp: 2020-05-14 16:56:27Z",
    "error_codes": [
        9002331
    ],
    "timestamp": "2020-05-14 16:56:27Z",
    "trace_id": "1c4047e6-98a8-4615-9a0c-4b0dc9ba5600",
    "correlation_id": "a6733520-6df9-422a-a6b4-e8f4e2de1265",
    "interval": 5,
    "expires_in": 1800,
    "expires_at": 1589477187.9909642,
    "_correlation_id": "a6733520-6df9-422a-a6b4-e8f4e2de1265"
}

推荐答案

这种方式已解决

That error message suggests you to create your authority string as   
authority = "https://login.microsoftonline.com/consumers",  

因为您使用的是个人应用程序"的client_id.更改该权限,即可继续.

because you were using the client_id of a "personal app". Change that authority, and you can proceed.

这篇关于我应该选择哪种类型的应用程序/身份验证流程来使用Python脚本和个人Microsoft帐户读取云OneNote内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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