Access Project online api on sharepoint online on c#

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

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

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

  1. string url = $"https://login.microsoftonline.com/{myTenantId}/oauth2/v2.0/token";
  2. var formData = new NameValueCollection();
  3. formData.Add("client_id", clientId);
  4. formData.Add("client_secret", clientSecret);
  5. formData.Add("username", username);
  6. formData.Add("password", password);
  7. formData.Add("scope", $"https://{myAzure}.sharepoint.com/.default");
  8. formData.Add("grant_type", "password");
  9. string encodedFormData = string.Join("&", Array.ConvertAll(formData.AllKeys, key =>
  10. string.Format("{0}={1}", Uri.EscapeDataString(key), Uri.EscapeDataString(formData[key]))));
  11. var request = (HttpWebRequest)WebRequest.Create(url);
  12. request.Method = "POST";
  13. request.ContentType = "application/x-www-form-urlencoded";
  14. byte[] formDataBytes = Encoding.UTF8.GetBytes(encodedFormData);
  15. request.ContentLength = formDataBytes.Length;
  16. using (var requestStream = request.GetRequestStream())
  17. {
  18. requestStream.Write(formDataBytes, 0, formDataBytes.Length);
  19. }
  20. using (var response = (HttpWebResponse)request.GetResponse())
  21. {
  22. using (var responseStream = response.GetResponseStream())
  23. {
  24. if (responseStream != null)
  25. {
  26. using (var reader = new System.IO.StreamReader(responseStream))
  27. {
  28. string responseContent = reader.ReadToEnd();
  29. JObject jsonResponse = JObject.Parse(responseContent);
  30. string accessToken = (string)jsonResponse["access_token"];
  31. using (HttpClient client = new HttpClient())
  32. {
  33. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
  34. HttpResponseMessage response2 = await client.GetAsync($"https://{myAzure}.sharepoint.com/sites/{myPWASite}/_api/ProjectData/Ressources");
  35. if (response2.IsSuccessStatusCode)
  36. {
  37. string responseBody = await response2.Content.ReadAsStringAsync();
  38. Console.WriteLine(responseBody);
  39. }
  40. else
  41. {
  42. Console.WriteLine("Erreur: " + response.StatusCode);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }

解释性文档

英文:

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

  1. string url = $"https://login.microsoftonline.com/{myTenantId}/oauth2/v2.0/token";
  2. var formData = new NameValueCollection();
  3. formData.Add("client_id", clientId);
  4. formData.Add("client_secret", clientSecret);
  5. formData.Add("username", username);
  6. formData.Add("password", password);
  7. formData.Add("scope", $"https://{myAzure}.sharepoint.com/.default");
  8. formData.Add("grant_type", "password");
  9. string encodedFormData = string.Join("&", Array.ConvertAll(formData.AllKeys, key =>
  10. string.Format("{0}={1}", Uri.EscapeDataString(key), Uri.EscapeDataString(formData[key]))));
  11. var request = (HttpWebRequest)WebRequest.Create(url);
  12. request.Method = "POST";
  13. request.ContentType = "application/x-www-form-urlencoded";
  14. byte[] formDataBytes = Encoding.UTF8.GetBytes(encodedFormData);
  15. request.ContentLength = formDataBytes.Length;
  16. using (var requestStream = request.GetRequestStream())
  17. {
  18. requestStream.Write(formDataBytes, 0, formDataBytes.Length);
  19. }
  20. using (var response = (HttpWebResponse)request.GetResponse())
  21. {
  22. using (var responseStream = response.GetResponseStream())
  23. {
  24. if (responseStream != null)
  25. {
  26. using (var reader = new System.IO.StreamReader(responseStream))
  27. {
  28. string responseContent = reader.ReadToEnd();
  29. JObject jsonResponse = JObject.Parse(responseContent);
  30. string accessToken = (string)jsonResponse["access_token"];
  31. using (HttpClient client = new HttpClient())
  32. {
  33. client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
  34. HttpResponseMessage response2 = await client.GetAsync($"https://{myAzure}.sharepoint.com/sites/{myPWASite}/_api/ProjectData/Ressources");
  35. if (response2.IsSuccessStatusCode)
  36. {
  37. string responseBody = await response2.Content.ReadAsStringAsync();
  38. Console.WriteLine(responseBody);
  39. }
  40. else
  41. {
  42. Console.WriteLine("Erreur: " + response.StatusCode);
  43. }
  44. }
  45. }
  46. }
  47. }
  48. }

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:

确定