身份验证流程来使用

身份验证流程来使用

本文介绍了我应该选择什么类型的应用程序/身份验证流程来使用 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 内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:57