Partner Center API bulk migration tool authentication HTTP-403 after successful login

Benno Steinegger 1 Reputation point
2022-08-30T07:31:14.91+00:00

I cloned the official microsoft parnter center migration api repo & installed all the required tools. Afterwards I build the project without any problems. I have chosen option 1 to export all customers. After logging in in my browser, the website said something like: successful login, you can close the tab. But afterwards a System.Net.Http.HttpRequestException: 'Response status code does not indicate success: 403 (Forbidden).' gets thrown.
The (sandbox-) account has admin permissions.

   do  
           {  
               var request = new HttpRequestMessage(HttpMethod.Get, route);  
               request.Headers.Add("MS-CorrelationId", Guid.NewGuid().ToString());  
               if (!string.IsNullOrWhiteSpace(continuationToken))  
               {  
                   request.Headers.Add("MS-ContinuationToken", continuationToken);  
                   route = $"{Routes.GetCustomers}&seekOperation=next";  
               }  
     
               request.RequestUri = new Uri(route);  
     
               var response = await httpClient.SendAsync(request).ConfigureAwait(false);  
               if (response.StatusCode == HttpStatusCode.BadRequest)  
               {  
                   var customerErrorResponse = await response.Content.ReadFromJsonAsync<CustomerErrorResponse>();  
                   //// If we get error code 2000 then it means we fetched all the records.  
                   if (customerErrorResponse?.Code == 2000)  
                   {  
                       break;  
                   }  
               }  
     
               if (response.StatusCode == HttpStatusCode.Unauthorized)  
               {  
                   authenticationResult = await this.tokenProvider.GetTokenAsync();  
                   httpClient.DefaultRequestHeaders.Clear();  
                   httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", authenticationResult.AccessToken);  
                   httpClient.DefaultRequestHeaders.Add(Constants.PartnerCenterClientHeader, Constants.ClientName);  
                   request = new HttpRequestMessage(HttpMethod.Get, route);  
                   request.Headers.Add("MS-CorrelationId", Guid.NewGuid().ToString());  
                   request.Headers.Add("MS-ContinuationToken", continuationToken);  
     
                   response = await httpClient.SendAsync(request).ConfigureAwait(false);  
               }  
     
               response.EnsureSuccessStatusCode();  


The detailed error message is:   
`System.Net.Http.HttpRequestException  
  HResult=0x80131500  
  Message=Response status code does not indicate success: 403 (Forbidden).  
  Source=System.Net.Http  
  StackTrace:  
   at System.Net.Http.HttpResponseMessage.EnsureSuccessStatusCode()  
   at NCEBulkMigrationTool.CustomerProvider.<ExportCustomersAsync>d__2.MoveNext() in C:\Users\bst\Documents\projects\Partner-Center-Tool\NCEBulkMigrationTool\CustomerProvider.cs:line 81  
   at Program.<<<Main>$>g__RunAsync|0_1>d.MoveNext() in C:\Users\bst\Documents\projects\Partner-Center-Tool\NCEBulkMigrationTool\Program.cs:line 98  
   at Program.<<Main>$>d__0.MoveNext() in C:\Users\bst\Documents\projects\Partner-Center-Tool\NCEBulkMigrationTool\Program.cs:line 60  

I am using Visual Studio 2022 and .NET 6

Microsoft Partner Center API
Microsoft Partner Center API
Microsoft Partner Center: A Microsoft website for partners that provides access to product support, a partner community, and other partner services.API: A software intermediary that allows two applications to interact with each other.
313 questions
{count} votes