我必须遗漏一些明显的东西。发布到测试版API https://graph.microsoft.com/beta/invitations(api ref:https://graph.microsoft.io/en-us/docs/api-reference/beta/api/invitation_post)时,我会
{ "错误":{ "代码":" UnknownError", "消息":"", " innerError":{ " request-id":" e41b0eab-c39c-4cf8-9034-341c81fc722c", " date":" 2017-01-14T19:26:55" } } }
这是我的代码:
namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
GraphClient g = new GraphClient();
Console.WriteLine(g.SendPost(g.authContext, g.credential).Result);
}
}
public class GraphClient
{
public AuthenticationContext authContext;
public ClientCredential credential;
public GraphClient()
{
this.authContext = new AuthenticationContext("https://login.microsoftonline.com/MYTENANT.onmicrosoft.com");
this.credential = new ClientCredential("MYCLIENTID", "MYCLIENTSECRET");
}
public async Task<string> SendPost(AuthenticationContext authContext, ClientCredential credential)
{
AuthenticationResult result = await authContext.AcquireTokenAsync("https://graph.microsoft.com", credential);
HttpClient http = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "https://graph.microsoft.com/beta/invitations");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
request.Content = new StringContent("{\"invitedUserEmailAddress\": \"MYEMAIL@MYDOMAIN.COM\",\"inviteRedirectUrl\": \"https://MYWEBSITE.COM\"}", Encoding.UTF8, "application/json");
HttpResponseMessage response = await http.SendAsync(request);
return await response.Content.ReadAsStringAsync();
}
}
}
谢谢!我可以做其他/ beta命令就好了。例如,GETting https://graph.microsoft.com/beta/users会按预期在我的租户中返回用户列表。
-Dan
答案 0 :(得分:0)
根据日志,您收到401错误响应,这意味着调用者未被授予调用API所需的权限。您需要按照此处的指导进行操作:https://graph.microsoft.io/en-us/docs/api-reference/beta/api/invitation_post表示您需要Directory.ReadWrite.All或User.ReadWrite.All(尽管我不确定后者是否有效或可用现在)。
我们还提交了一个错误来修复此错误消息(抱歉 - 我们确实需要在此处做得更好)。
希望这有帮助,