尝试访问 Dynamics 365 Web Api 时,Excel 插件不断收到 401 错误

如何解决尝试访问 Dynamics 365 Web Api 时,Excel 插件不断收到 401 错误

我正在尝试使用 Typescript 在 React 中开发一个单词插件,它允许我访问 dynamics 365 Web Api。我实际发现的最佳示例是在示例 excel 添加中,它使用 MSAL 2.0 访问图形 api https://github.com/OfficeDev/PnP-OfficeAddins/tree/master/Samples/auth/Office-Add-in-Microsoft-Graph-React。如果我可以在修改示例代码后成功获取到我的 Dynamics 365 api 的请求,我会将代码移植到我的 Word 插件中。

但是,我在尝试访问 https://saltrial.crm.dynamics.com/api/data/v9.1/
时不断收到 401 错误 当我简单地将其粘贴到浏览器中时,我得到了回复,但在添加中我得到 401 未经授权。当我将从 add in 收到的令牌粘贴到带有 Bearer + token 标头的邮递员时,我也得到了 401 未授权。但是当我选择时,我已经获得了一个成功的访问令牌

enter image description here

我将向您展示我在 Azure AD 中的设置......我有一个客户端机密设置,但我没有在我的插件代码中使用它。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

清单 { "id": "35c3a758-0edb-45f6-a97d-9c7180decd73","acceptMappedClaims": null,"accessTokenAcceptedVersion": 2,“插件”:[], “allowPublicClient”:真, "appId": "4f7xxxxxxxxxxxxxxxxxxxxxxxx310bf0",“应用角色”:[], “oauth2AllowUrlPathMatching”:假, "createdDateTime": "2021-01-24T07:09:12Z",“disabledByMicrosoftStatus”:空, "groupMembershipClaims": null,“标识符Uris”:[ "api://localhost:3000/4fxxxxxxxxxxxxxxxxxxxxxxxxxx" ],“信息网址”:{ “服务条款”:空, “支持”:空, “隐私”:空, “营销”:空 },"keyCredentials": [],"knownClientApplications": [],“logoUrl”:空, “logoutUrl”:空, "name": "TrySSO",“oauth2AllowIdTokenImplicitFlow”:真, “oauth2AllowImplicitFlow”:真, “oauth2Permissions”:[ { "adminConsentDescription": "启用 Office 以与当前用户相同的权限调用加载项的 Web API。","adminConsentDisplayName": "Office 可以充当用户","id": "5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f",“已启用”:真, “朗”:空, "origin": "应用程序","type": "用户","userConsentDescription": "启用 Office 以您拥有的相同权限调用加载项的 Web API。","userConsentDisplayName": "Office 可以充当你",“值”:“access_as_user” } ],“oauth2RequirePostResponse”:假, “可选声明”:空, “组织限制”:[], “家长控制设置”:{ "countriesBlockedForMinors": [],"legalAgeGroupRule": "允许" },“密码凭据”:[ { “customKeyIdentifier”:空, "endDate": "2299-12-31T05:00:00Z","keyId": "570axxxxxxxxxxxxxxxxxxxxxxxxxxxxbcd1","startDate": "2021-01-28T04:11:05.086Z",“值”:空, "createdOn": "2021-01-28T04:11:06.027737Z","提示": "mm-","displayName": "wordaddinsecret" } ],“预授权应用程序”:[ { "appId": "ea5a67f6-b6f3-4338-b240-c655ddc3cc8e",“权限ID”:[ “5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f” ] },{ "appId": "d3590ed6-52b3-4102-aeff-aad2292ab01c",{ "appId": "57fb890c-0dab-4253-a5e0-7188c88b2bb4",{ "appId": "bc59ab01-8403-45c6-8796-ac3ef710b3e3",“权限ID”:[ “5b3a4e4a-e55e-45ba-820b-ea16efbe3d5f” ] } ],"publisherDomain": "salnewtrial.onmicrosoft.com","replyUrlsWithType": [ { "url": "https://localhost:3000/login/login.html",“类型”:“温泉” },{ "url": "https://login.microsoftonline.com/common/oauth2/nativeclient",“类型”:“已安装的客户端” },{ "url": "https://localhost:3000/login.html",“类型”:“网页” } ],“必需的资源访问”:[ { "resourceAppId": "00000007-0000-0000-c000-000000000000",“资源访问”:[ { "id": "78ce3f0f-a1ce-49c2-8cde-64b5c0896db4",“类型”:“范围” } ] },{ "resourceAppId": "00000003-0000-0000-c000-000000000000",“资源访问”:[ { "id": "14dad69e-099b-42c9-810b-d002981feec1",“类型”:“范围” },{ "id": "7427e0e9-2fba-42fe-b0c0-848c9e6a8182",{ "id": "37f7f235-527c-4136-accd-4a02d197296e",{ "id": "e1fe6dd8-ba31-4d61-89e7-88639da4683d",“类型”:“范围” } ] } ],“samlMetadataUrl”:空, “signInUrl”:空, "signInAudience": "AzureADMyOrg",“标签”:[], “tokenEncryptionKeyId”:空 }

在我的login.ts里面的excel添加

  (() => {
  // The initialize function must be run each time a new page is loaded
  Office.initialize = () => {
    const msalInstance = new PublicClientApplication({
      auth: {
        clientId: "4f7f40ec-xxxxxxxxx-5d6b18310bf0",authority: "https://login.microsoftonline.com/cd77a053-xxxxxxxxxx3402c0fd62",*This is tenant id
        redirectUri: "https://localhost:3000/login/login.html",// Must be registered as "spa" type
      },cache: {
        cacheLocation: "localStorage",// needed to avoid "login required" error
        storeAuthStateInCookie: true,// recommended to avoid certain IE/Edge issues
      },});

    // handleRedirectPromise should be invoked on every page load
    msalInstance
      .handleRedirectPromise()
      .then((response) => {
        // If response is non-null,it means page is returning from AAD with a successful response
        if (response) {
          Office.context.ui.messageParent(
            JSON.stringify({ status: "success",result: response.accessToken })
          );
        } else {
          // Otherwise,invoke login
          msalInstance.loginRedirect({
            scopes: [
              "user.read","files.read.all","https://saltrial.crm.dynamics.com//user_impersonation",],});
        }
      })
      .catch((error) => {
        const errorData: string = `errorMessage: ${error.errorCode}
                                   message: ${error.errorMessage}
                                   errorCode: ${error.stack}`;
        Office.context.ui.messageParent(
          JSON.stringify({ status: "failure",result: errorData })
        );
      });
  };
})();

这是我在 Add in 中的 API 调用

    import axios from 'axios';

export const getGraphData = async (url: string,accesstoken: string) => {
    const response = await axios({
        url: url,method: 'get',headers: {'Authorization': `Bearer ${accesstoken}`,'OData-MaxVersion': '4.0','OData-Version': '4.0',"Accept": "application/json","Content-Type": "application/json; charset=utf-8",}
      });
    return response;
};

调用我的 api 函数并传递范围和动态 url 的代码。我控制台记录访问令牌并将其放入带有标题承载令牌的邮递员,我得到 401。此外,加载项在任务窗格中显示 401 未经授权......忽略任何引用图形 API 的函数命名,我是点击动态 365 api 并希望我没有收到 401 错误。谢谢。

getFileNames = async () => {
    this.setState({ fileFetch: "fetchInProcess" });
    getGraphData(
      // Get the `name` property of the first 3 Excel workbooks in the user's OneDrive.
      "https://saltrial.crm.dynamics.com/api/data/v9.1/WhoAmI",this.accessToken
    )
      .then(async (response) => {
        await writeFileNamesToWorksheet(response,this.displayError);
        this.setState({ fileFetch: "fetched",headerMessage: "Success" });
      })
      .catch((requestError) => {
        // If this runs,then the `then` method did not run,so this error must be
        // from the Axios request in getGraphData,not the Office.js in
        // writeFileNamesToWorksheet
        console.log("Access Token >>>>>>>>>>>>>>>>> ",this.accessToken);
        this.displayError(requestError);
      });
  };

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res