none
Invalid audience Uri when getting a File with SharePoint client RRS feed

  • General discussion

  • Hi

    I want to create a file in the one drive using the office 365 API (i am using a ASP.net application), but for some reason my code stopped working. I have all the data from my Azure AD and here is how I try to create a file:

    i am using AcquireTokenByAuthorizationCode to get the token. This is how I request the authorization code:

    this._authenticationContext = new AuthenticationContext("https://login.windows.net/" + _tenantId, true);
    
    Uri redirect = this._authenticationContext.GetAuthorizationRequestURL(
        "https://api.office.com/discovery/",
        SettingsHelper.ClientId,
        new Uri(currentUrl),
        UserIdentifier.AnyUser,
        String.Empty
    );

    And this is how I get the token:

    // this._authenticationContext = new AuthenticationContext("https://login.windows.net/" + _tenantId, true);
    AuthenticationResult result = this._authenticationContext.AcquireTokenByAuthorizationCode(
        authorizationCode, 
        new Uri(Utils.GetCurrentUrl()), 
        AuthenticationHelper.GetClientCredential());

    This all works good I get a token + refresh token. Then I create the SharePoint client like:

    // this.GetUserTokenAsync() returns the token from the AcquireTokenByAuthorizationCode part of the code
    DiscoveryClient discoveryClient = new DiscoveryClient(SettingsHelper.DiscoveryServiceEndpointUri, () => this.GetUserTokenAsync());
    CapabilityDiscoveryResult result = await discoveryClient.DiscoverCapabilityAsync(capability);
    SharePointClient client = new SharePointClient(result.ServiceEndpointUri, () => this.GetUserTokenAsync());

    Until here it's all good I get the SharePointClient but when I try to call any function on the client for example: IItem item = await client.Files.GetByPathAsync(fileName);

    I get: {"error":"invalid_client","error_description":"Invalid audience Uri 'https:\/\/api.office.com\/discovery\/'."}

    My code use to work 2 weeks ago but now it doesn't can someone help me?

    Friday, July 24, 2015 11:58 PM

All replies

  • What is the value of "capability"? Apparently the value of "result.ServiceEndpointUri" is "https://api.office.com/discovery", but that's incorrect. It must be the URL of your SharePoint site.

    This error means that the token is not valid for the resource you are accessing.

    I've had a similar issue in the ADAL JS library. See my blog post, this may be relevant.


    Please remember to mark your question as answered & Vote helpful
    Check my blog or follow me on Twitter

    Wednesday, August 5, 2015 10:47 PM