英文:
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);
}
}
}
}
}
}
解释性文档
- https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc
- https://techcommunity.microsoft.com/t5/project-support-blog/app-user-authentication-and-project-online-development/ba-p/1925443
- https://learn.microsoft.com/en-us/azure/data-factory/connector-odata?tabs=data-factory#copy-data-from-project-online
英文:
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
- https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-oauth-ropc
- https://techcommunity.microsoft.com/t5/project-support-blog/app-user-authentication-and-project-online-development/ba-p/1925443
- https://learn.microsoft.com/en-us/azure/data-factory/connector-odata?tabs=data-factory#copy-data-from-project-online
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论