title: Sharepoint Rest API Authentication issues with Access Token Header
body: <p>I'm trying to implement a C# program to connect to Sharepoint API through modern authentication (Client ID\ Client Secret).</p>
  <p>I've registered an APP with Sharepoint overall permissions on Azure Active Directory, in order to generate Client Id and Client Secret.</p>
  <p>Next steps should be retrieval of the Access Token from the Microsoft login page, and then construction of all following requests using the bearing token I've generated.</p>
  <p>Retrieval of the Access Token just works fine. The problem is when I try to include the token in the authorization header on the following calls.</p>
  <p>I always get 401 Unhautorized when building my requests from code. Debugging the response content, what I get is <code>&quot;x-ms-diagnostics: 3000006;reason=&quot;Token contains invalid signature&quot;; category&quot;invalid_client&quot;</code>.
  Instead if I try to replicate the call in Postman I get the following error &quot;{&quot;error_description&quot;:&quot;Unsupported security token.&quot;}&quot;.</p>
  <p>I provide my code below. Does anybody knows what is going on?</p>
  <pre><code>     var b2cAuthUri = &quot;https://login.microsoftonline.com/&quot; + tenantId + &quot;/oauth2/v2.0/token&quot;;
      
                  var client = new HttpClient();
      
                  var dict = new Dictionary&lt;string, string&gt;();
                  dict.Add(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
                  dict.Add(&quot;grant_type&quot;, &quot;client_credentials&quot;);
                  dict.Add(&quot;client_id&quot;, clientId);
                  dict.Add(&quot;client_secret&quot;, clientSecret);
                  dict.Add(&quot;scope&quot;, scope);
      
                  // Execute post method
                  using (var methodResp = client.PostAsync(b2cAuthUri, new FormUrlEncodedContent(dict)))
                  {
      
                      var callResult = methodResp.Result.Content.ReadAsStringAsync().Result;
                      if (!string.IsNullOrEmpty(callResult))
                      {
                          //I have my Access Token here :)
                          using (MemoryStream DeSerializememoryStream = new MemoryStream())
                          {
                             
      
                              //initialize DataContractJsonSerializer object and pass custom token class type to it
                              DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AccessToken));
      
                              //user stream writer to write JSON string data to memory stream
                              StreamWriter writer = new StreamWriter(DeSerializememoryStream);
                              writer.Write(callResult);
                              writer.Flush();
      
                              DeSerializememoryStream.Position = 0;
                              //get the Desrialized data in object of type Student
                              AccessToken SerializedObject = (AccessToken)serializer.ReadObject(DeSerializememoryStream);
                              var tokenBytes = System.Text.Encoding.UTF8.GetBytes(SerializedObject.access_token);
                              //64bit serialized token
                              var tokenBase64 = System.Convert.ToBase64String(tokenBytes);
      
                              //Here I try to make a call with the access token as header
                              var testURI = &quot;https://myorg.sharepoint.com/sites/crmkb/_api/web/lists&quot;;
      
                              HttpWebRequest testReq = (HttpWebRequest)HttpWebRequest.Create(testURI);
                              testReq.Headers.Add(HttpRequestHeader.Authorization, &quot;Bearer &quot; + tokenBase64);
                              testReq.Method = &quot;GET&quot;;
                              //This fails on 401 code
                              HttpWebResponse response = (HttpWebResponse)testReq.GetResponse();
      
      
                          }
                      }
                  }
  </code></pre>