title: Discord api returns 401 unauthorized when trying signin via Azure AD B2C custom policy
body: <p>I'm building up an app that uses Discord OAuth2 to various purposes including user identity, but have encountered an issue that Discord api returns 401 (unauthorized) error when a user tries to signin.</p>
  
  <p>I have a B2C tenant that have the custom policy that contain the <code>ClaimsProbider</code> written as:</p>
  
  <pre><code>  &lt;ClaimsProvider&gt;
        &lt;!-- The following Domain element allows this profile to be used if the request comes with domain_hint 
             query string parameter, e.g. domain_hint=Discord.com  --&gt;
        &lt;Domain&gt;Discord.com&lt;/Domain&gt;
        &lt;DisplayName&gt;Discord&lt;/DisplayName&gt;
        &lt;TechnicalProfiles&gt;
          &lt;TechnicalProfile Id="Discord-OAUTH"&gt;
            &lt;!-- The text in the following DisplayName element is shown to the user on the claims provider 
                 selection screen. --&gt;
            &lt;DisplayName&gt;Discord&lt;/DisplayName&gt;
            &lt;Protocol Name="OAuth2" /&gt;
            &lt;Metadata&gt;
              &lt;Item Key="ProviderName"&gt;Discord&lt;/Item&gt;
              &lt;Item Key="authorization_endpoint"&gt;https://discordapp.com/api/oauth2/authorize&lt;/Item&gt;
              &lt;Item Key="AccessTokenEndpoint"&gt;https://discordapp.com/api/oauth2/token&lt;/Item&gt;
              &lt;Item Key="ClaimsEndpoint"&gt;https://discordapp.com/api/users/@me&lt;/Item&gt;
              &lt;Item Key="HttpBinding"&gt;POST&lt;/Item&gt;
              &lt;Item Key="UsePolicyInRedirectUri"&gt;0&lt;/Item&gt;
              &lt;Item Key="client_id"&gt;[my key]&lt;/Item&gt;
              &lt;Item Key="scope"&gt;identify email connections guilds.join rpc rpc.api rpc.notifications.read messages.read&lt;/Item&gt;
              &lt;Item Key="AccessTokenResponseFormat"&gt;json&lt;/Item&gt;
            &lt;/Metadata&gt;
            &lt;CryptographicKeys&gt;
              &lt;Key Id="client_secret" StorageReferenceId="B2C_1A_DiscordSecret" /&gt;
            &lt;/CryptographicKeys&gt;
            &lt;InputClaims /&gt;
            &lt;OutputClaims&gt;
              &lt;OutputClaim ClaimTypeReferenceId="socialIdpUserId" DefaultValue="id" /&gt;
              &lt;OutputClaim ClaimTypeReferenceId="givenName" DefaultValue="first_name" /&gt;
              &lt;OutputClaim ClaimTypeReferenceId="surname" DefaultValue="last_name" /&gt;
              &lt;OutputClaim ClaimTypeReferenceId="displayName" DefaultValue="name" /&gt;
              &lt;OutputClaim ClaimTypeReferenceId="email" DefaultValue="email"/&gt;
              &lt;OutputClaim ClaimTypeReferenceId="identityProvider" DefaultValue="Discord.com" /&gt;
              &lt;OutputClaim ClaimTypeReferenceId="authenticationSource" DefaultValue="socialIdpAuthentication" /&gt;
            &lt;/OutputClaims&gt;
            &lt;OutputClaimsTransformations&gt;
              &lt;OutputClaimsTransformation ReferenceId="CreateRandomUPNUserName" /&gt;
              &lt;OutputClaimsTransformation ReferenceId="CreateUserPrincipalName" /&gt;
              &lt;OutputClaimsTransformation ReferenceId="CreateAlternativeSecurityId" /&gt;
            &lt;/OutputClaimsTransformations&gt;
            &lt;UseTechnicalProfileForSessionManagement ReferenceId="SM-SocialLogin" /&gt;
          &lt;/TechnicalProfile&gt;
        &lt;/TechnicalProfiles&gt;
      &lt;/ClaimsProvider&gt;
  </code></pre>
  
  <p>The error I collected via Application Insights is:</p>
  
  <pre><code>{
      ""Kind"": ""HandlerResult"",
      ""Content"": {
        ""Result"": true,
        ""RecorderRecord"": {
          ""Values"": [
            {
              ""Key"": ""SendErrorTechnicalProfile"",
              ""Value"": ""OAuth2ProtocolProvider""
            },
            {
              ""Key"": ""Exception"",
              ""Value"": {
                ""Kind"": ""Handled"",
                ""HResult"": ""80131500"",
                ""Message"": ""An exception was caught when making a request to URL \""https://discordapp.com/api/users/@me\"" using method \""Get\"". The exception status code was \""ProtocolError\"" with the following message: {scrubbed}."",
                ""Data"": {},
                ""Exception"": {
                  ""Kind"": ""Handled"",
                  ""HResult"": ""80131509"",
                  ""Message"": ""The remote server returned an error: (401) Unauthorized."",
                  ""Data"": {}
                }
              }
            }
          ]
        }
      }
  </code></pre>
  
  <p><a href="https://stackoverflow.com/questions/43269517/discord-add-guild-member-401-error-despite-apparently-valid-access-token">Here</a>'s a solution to avoid getting 401 when requesting a claim to the api, but I'm not sure how it works in this situation with a custom policy on B2C tenant.</p>
  
  <p>Could anyone please provide a suggestion to this issue?</p>