【问题标题】:Add Member to Office 365 group with JSON/JavaScript使用 JSON/JavaScript 将成员添加到 Office 365 组
【发布时间】:2016-04-13 17:56:21
【问题描述】:

我正在尝试将单击 SharePoint(在线)网站中的按钮的用户添加到 Office 365 组。我知道这可以通过 JSON 使用 Add Member API 来完成。

https://github.com/OfficeDev/microsoft-graph-docs/blob/master/api-reference/v1.0/api/group_post_members.md

然而,当谈到 JSON 并不断搞乱 POST 函数时,我真的很缺乏经验。这是我目前拥有的代码,逗号之前的所有内容都可以正常工作。

function showButton() {

    $('btn-1').on('click', function(event) {
        var userProfileProperties
        var clientContext = new SP.ClientContext.get_current();
        var peopleManager = new SP.UserProfiles.PeopleManager(clientContext);
        userProfileProperties = peopleManager.getMyProperties();
        clientContext.load(userProfileProperties);
        clientContext.executeQueryAsync(onSuccess, onFail);

        function onSuccess(){
            accountProperties = userProfileProperties.get_userProfileProperties();
            accountId = accountProperties['msOnline-ObjectId'];
            //JSON Query
            jQuery.ajax({   
                url: "https://mysite.sharepoint.com/groups/groupID/members/$ref";
                method: "POST";
                contentType: "application/json";
                dataType: 'json',
                {
                    "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/" + accountId
                };
            });
        };  
        function onFail(){
            alert(failed);
        };
    });
};

【问题讨论】:

    标签: javascript json sharepoint office365 sharepoint-online


    【解决方案1】:

    在您的文档中,您会发现请求标头中需要身份验证令牌。没有身份验证令牌,您将收到如下错误:

    "code": "InvalidAuthenticationToken", "message": "Bearer access token is empty."
    

    作为解决方案,您可以尝试以下步骤:

    1.在 Azure AD 中注册一个 javascript 应用程序并配置您的应用程序以允许 OAuth 2.0 隐式授权流。使用 OAuth 2.0 隐式授权流获取令牌。使用隐式授权,您的应用程序通过将用户发送到授权 URL 为当前登录的用户从 Azure AD 请求访问令牌,在该 URL 用户使用其 Office 365 凭据登录,然后使用访问令牌重定向回应用程序在网址中。

    2.为 Graph API 添加权限。

    3.在线将html页面添加到您的共享点(使用资源管理器模式)。

    4.编辑html,编写以下函数获取访问令牌:

     function requestToken() { 
       // Change clientId and replyUrl to reflect your app's values 
       // found on the Configure tab in the Azure Management Portal. 
       // Also change {your_subdomain} to your subdomain for both endpointUrl and resource. 
       var clientId = '3dadb44e-feaa-4158-90f5-e129e15db66d';//ID of your App in Azure
       var replyUrl = 'https://o365e3w15.sharepoint.com/sites/XXX/app.html';  //my sharepoint page that requests 
       //an oauth 2 authentification and data
       //It is also referenced in the REPLY URL field of my App in Azure
       var endpointUrl = 'https://graph.microsoft.com/v1.0/me/messages';
       var resource = "https://graph.microsoft.com/";
       var authServer  = 'https://login.windows.net/common/oauth2/authorize?';  
       //var authServer  =  'https://login.microsoftonline.com/common/oauth2/authorize?';//this works either
       var responseType = 'token'; 
    
       var url = authServer + 
            "response_type=" + encodeURI(responseType) + "&" + 
            "client_id=" + encodeURI(clientId) + "&" + 
            "resource=" + encodeURI(resource) + "&" + 
            "redirect_uri=" + encodeURI(replyUrl); 
    
       window.location = url; 
    }
    
    1. 之后,您可以通过 ajax 调用 graph api 端点来获取/发布请求,例如,获取当前用户的消息:

      var endpointUrl = "https://graph.microsoft.com/v1.0/me/messages";
      
      var xhr = new XMLHttpRequest(); 
      xhr.open("GET", endpointUrl); 
      var myToken = getToken();
      // The APIs require an OAuth access token in the Authorization header, formatted like this: 
      //'Authorization: Bearer <token>'. 
      xhr.setRequestHeader("Authorization", "Bearer " + myToken); 
      
      // Process the response from the API.  
      xhr.onload = function () { 
        if (xhr.status == 200) { 
          //alert('data received');
          var message="";
          var object = JSON.parse(xhr.response); 
          for(i=0;i<object.value.length;i++){
          message+='Subject: ' + object.value[i].subject + '<br>';    
         }
         document.getElementById("results").innerHTML = message;
        } else { } 
      }
      // Make request.
      xhr.send(); 
      
    2. 通过在 iframe 标记中调用此 app.html 将其显示到任何 SharePoint Webpart 页面中。

    您将在this article 中找到所有详细步骤,我已经测试并在我身边工作正常。

    【讨论】:

    • 我知道这将如何获得 OAuth 令牌,但我无法为每个访问我的按钮所在页面的用户调用令牌。
    猜你喜欢
    • 1970-01-01
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多