Resource Not Found While fetching contacts from MS

2019-07-08 10:28发布

问题:

I tried below github sample for Azure AD Authentication and successfully getting access token with proper call back URL.

https://github.com/Microsoft/BotBuilder-Samples/tree/master/samples/csharp_dotnetcore/18.bot-authentication

Modified and Configured Web.config Keys as per my application created in https://apps.dev.microsoft.com

  <add key="ActiveDirectory.Mode" value="v1" />
<add key="ActiveDirectory.ResourceId" value="https://graph.microsoft.com" />
<add key="ActiveDirectory.EndpointUrl" value="https://login.microsoftonline.com" />
<add key="ActiveDirectory.Tenant" value="xyz-xyz" />
<add key="ActiveDirectory.ClientId" value="xyz-xyz" />
<add key="ActiveDirectory.ClientSecret" value="xyz-xyz" />
 <add key="ActiveDirectory.RedirectUrl" value="http://localhost:3979/api/Callback" />
<add key="ActiveDirectory.Scopes" value="Calendars.Read,Calendars.ReadWrite,Contacts.Read,Contacts.ReadWrite,Mail.Read,Mail.ReadWrite,User.Read" />

I had crossed checked access token that i received jwt.ms. It is a valid token with scopes defined exactly as i defined in web.config.

I am able to get User Profile data from API by passing token but while trying to call "Contacts" of user, I am getting below error.

{
"error": {
    "code": "ResourceNotFound",
    "message": "Resource could not be discovered.",
    "innerError": {
        "request-id": "6f0f3ec9-76c9-4662-ac25-0bc73f887268",
        "date": "2019-03-02T17:23:35"
    }
}

}

Code calling Contact API call.

   //Get Logged in user contacts
    public async Task<IUserContactsCollectionPage> GetMyContactsAsync()
    {
        var graphClient = GetAuthenticatedClient();
        IUserContactsCollectionPage contacts = await graphClient.Me.Contacts.Request().GetAsync();
        return contacts;
    }

FYI: Displayed Sign In card in emulator Authorize sample URL is like below

https://login.microsoftonline.com/a76b43b0-3088-4c56-ba0d-01f317b1f18c/oauth2/authorize?resource=https:%2F%2Fgraph.microsoft.com&client_id=XYZ&response_type=code&haschrome=1&redirect_uri=http:%2F%2Flocalhost:3979%2Fapi%2FCallback&x-client-SKU=PCL.Desktop&x-client-Ver=3.13.9.1126&x-client-CPU=x64&x-client-OS=Microsoft+Windows+NT+10.0.16299.0&state=<stateID>

回答1:

It might happens for many reason one of the very common issue is to required grant permission to your registered app. Make sure you have done following steps accordingly.

Step 1:

Login in to your portal and select your application under app registrations. Then click on settings see the screen shot below:

Step 2:

Once you clicked Settings new window will come up right side, click on Required Permission menu: see the screen shot below:

Step 3:

Select Required Permission Menu and assign your permission by clicking on Add button see the snap below:

Step 4:

Click on Select an API option see below:

Final Step:

Select your desired API and checked it which you are like to access for. Once finished the permission click on Done. After this step you can access your required resource / Scope from your application. Hope it will be helpful for you.Thank You. see the screen shot below: