Access Project online api on sharepoint online on c#

huangapple go评论70阅读模式
英文:

Access Project online api on sharepoint online on c#

问题

我有一个SharePoint(myAzure.sharepoint.com)。里面有一个PWA站点(Project Online)。PWA提供了OData API。该API的安全性要求用户是您的AD成员(而不是应用程序)。

我想要在C#中在Azure之外的地方(比如本地主机)使用该API。我需要在没有与用户的Web交互的情况下完成这个操作。

英文:

I have a sharepoint (myAzure.sharepoint.com). Inside I got a site PWA (project online). PWA offert OData Api. The security of that Api required a User member of your AD (not an app).

I want, in c#, to consumme the api outside azure (like localhost). I need to do it without interractive (web promp interraction with the user).

答案1

得分: 0

我找到的唯一方法是通过 "密码凭据"。

string url = $"https://login.microsoftonline.com/{myTenantId}/oauth2/v2.0/token";
var formData = new NameValueCollection();
formData.Add("client_id", clientId);
formData.Add("client_secret", clientSecret);
formData.Add("username", username);
formData.Add("password", password);
formData.Add("scope", $"https://{myAzure}.sharepoint.com/.default");
formData.Add("grant_type", "password");
string encodedFormData = string.Join("&", Array.ConvertAll(formData.AllKeys, key =>
string.Format("{0}={1}", Uri.EscapeDataString(key), Uri.EscapeDataString(formData[key]))));

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

byte[] formDataBytes = Encoding.UTF8.GetBytes(encodedFormData);
request.ContentLength = formDataBytes.Length;
using (var requestStream = request.GetRequestStream())
{
    requestStream.Write(formDataBytes, 0, formDataBytes.Length);
}

using (var response = (HttpWebResponse)request.GetResponse())
{
    using (var responseStream = response.GetResponseStream())
    {
        if (responseStream != null)
        {
            using (var reader = new System.IO.StreamReader(responseStream))
            {
                string responseContent = reader.ReadToEnd();
                JObject jsonResponse = JObject.Parse(responseContent);
                string accessToken = (string)jsonResponse["access_token"];

                using (HttpClient client = new HttpClient())
                {                                
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
                    HttpResponseMessage response2 = await client.GetAsync($"https://{myAzure}.sharepoint.com/sites/{myPWASite}/_api/ProjectData/Ressources");
                 
                    if (response2.IsSuccessStatusCode)
                    {
                        string responseBody = await response2.Content.ReadAsStringAsync();
                        Console.WriteLine(responseBody);
                    }
                    else
                    {
                        Console.WriteLine("Erreur: " + response.StatusCode);
                    }
                }
            }
        }
    }
}

解释性文档

英文:

The only way I found was by "password credential".

string url = $"https://login.microsoftonline.com/{myTenantId}/oauth2/v2.0/token";
var formData = new NameValueCollection();
formData.Add("client_id", clientId);
formData.Add("client_secret", clientSecret);
formData.Add("username", username);
formData.Add("password", password);
formData.Add("scope", $"https://{myAzure}.sharepoint.com/.default");
formData.Add("grant_type", "password");
string encodedFormData = string.Join("&", Array.ConvertAll(formData.AllKeys, key =>
string.Format("{0}={1}", Uri.EscapeDataString(key), Uri.EscapeDataString(formData[key]))));

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";

byte[] formDataBytes = Encoding.UTF8.GetBytes(encodedFormData);
request.ContentLength = formDataBytes.Length;
using (var requestStream = request.GetRequestStream())
{
	requestStream.Write(formDataBytes, 0, formDataBytes.Length);
}

using (var response = (HttpWebResponse)request.GetResponse())
{
	using (var responseStream = response.GetResponseStream())
	{
		if (responseStream != null)
		{
			using (var reader = new System.IO.StreamReader(responseStream))
			{
				string responseContent = reader.ReadToEnd();
				JObject jsonResponse = JObject.Parse(responseContent);
				string accessToken = (string)jsonResponse["access_token"];

				using (HttpClient client = new HttpClient())
				{                                
					client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
					HttpResponseMessage response2 = await client.GetAsync($"https://{myAzure}.sharepoint.com/sites/{myPWASite}/_api/ProjectData/Ressources");
				 
					if (response2.IsSuccessStatusCode)
					{
						string responseBody = await response2.Content.ReadAsStringAsync();
						Console.WriteLine(responseBody);
					}
					else
					{
						Console.WriteLine("Erreur: " + response.StatusCode);
					}
				}
			}
		}
	}
}

Explanatory documentation

huangapple
  • 本文由 发表于 2023年6月6日 01:29:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76408752.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定