尝试调用Microsoft Graph客户端时出现InvalidAuthenticationToken

时间:2016-11-03 12:22:58

标签: microsoft-graph

我正在关注Microsoft Graph repository on github提供的示例。在其中,它描述了使用需要访问令牌的DelegateAuthenticationProvider。 Marc LaFleur的primer on auth flow指定了一种获取访问令牌的方法,该访问令牌与guidance结合使用,可以使服务 - 服务身份验证工作。

即使我收到似乎是有效的访问令牌,当我通过图形客户端发出命令时,我从Microsoft Graph获得了一个ServiceException,

“{     代码:InvalidAuthenticationToken     消息:访问令牌验证失败。     内心错误 }“

我没有看到任何其他内部错误。

我做错了什么?

这是我提出的代码:

var clientId = "[app-guid]";
var clientSecret = "[secret-from-app-dashboard]";
var resource = "[app-guid]";
var baseUrl = "https://login.microsoftonline.com";
var loginUrl = "/[tenant guid]/oauth2/token";
using (var client = new HttpClient())
{
    client.BaseAddress = new Uri(baseUrl);
    var content = new FormUrlEncodedContent(new[]
    {
        new KeyValuePair<string, string>("grant_type", "client_credentials"),
        new KeyValuePair<string, string>("client_id", clientId),
        new KeyValuePair<string, string>("client_secret", clientSecret),
        new KeyValuePair<string, string>("resource", resource)
    });
    var result = client.PostAsync(loginUrl, content).Result;
    string resultContent = result.Content.ReadAsStringAsync().Result;
    var json = JObject.Parse(resultContent);

    accessToken = (string)json["access_token"];

} 
var header = new AuthenticationHeaderValue("Bearer", accessToken);
var graphserviceClient = new GraphServiceClient(
new DelegateAuthenticationProvider(
(requestMessage) =>
{
    requestMessage.Headers.Authorization = header;


    return Task.FromResult(0);
}));
try
{
    var users = await graphserviceClient.Users.Request().GetAsync();
    var user = users.First();
}
catch (Microsoft.Graph.ServiceException servex)
{
    Console.WriteLine(servex);
}

1 个答案:

答案 0 :(得分:0)

您将资源设置为指向您的网站,但它应指向您要访问的外部网络资源。试试这个:

echo DB db1 processing
 set MYFILENAME=db1_11-11-2016_0-54.bak
 set SERVERNAME=.
 echo <<<----------HERE MISSING MYFILENAME ----------<<<
 echo sqlcmd -B -E -S . -d master -Q "BACKUP DATABASE [zdvrhnika] TO DISK = N''
WITH INIT , NOUNLOAD , NAME = N'zdvrhnika backup', NOSKIP , STATS = 10, NOFORMAT
"