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 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("" + _tenantId, true);
    Uri redirect = this._authenticationContext.GetAuthorizationRequestURL(
        new Uri(currentUrl),

    And this is how I get the token:

    // this._authenticationContext = new AuthenticationContext("" + _tenantId, true);
    AuthenticationResult result = this._authenticationContext.AcquireTokenByAuthorizationCode(
        new Uri(Utils.GetCurrentUrl()), 

    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:\/\/\/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 "", 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