title: Google API - Invalid Credentials
body: <p>I am trying to make an iOS app where a user can rate a YouTube video using the <a href="https://developers.google.com/youtube/v3/docs/videos/rate" rel="nofollow noreferrer">YouTube API</a>. To do this, the user has to be authenticated. So, I used Google's <a href="https://developers.google.com/identity/sign-in/ios/start-integrating" rel="nofollow noreferrer">Google Sign-In</a> to authenticate my users. I followed the <a href="https://developers.google.com/identity/sign-in/ios/start-integrating" rel="nofollow noreferrer">implementation guide</a>, and it seems as if the user gets signed in. I can access data from the user such as their profile image, name, etc.</p>
  
  <p>Because I am using the <a href="https://developers.google.com/youtube/v3/docs/videos/rate" rel="nofollow noreferrer">YouTube API</a>, I need to add the YouTube scope to Google Sign-In before the user signs in. So, I do that like so...</p>
  
  <pre><code>GIDSignIn.sharedInstance().scopes = ["https://www.googleapis.com/auth/youtube"]
  </code></pre>
  
  <p>Now whenever a user signs in, it will request access to their YouTube channel.</p>
  
  <p><a href="https://i.stack.imgur.com/YEnAr.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YEnAr.png" alt="Screenshot of Google Sign-In page"></a></p>
  
  <p>It seems as if everything is working perfectly. But, now to rate the YouTube video. You can check out <a href="https://developers.google.com/youtube/v3/docs/videos/rate" rel="nofollow noreferrer">Google's documentation on how to rate a YouTube video</a>, but basically, this is what I have to do.</p>
  
  <pre><code>Alamofire.request("https://www.googleapis.com/youtube/v3/videos/rate", method: .post, parameters: ["access_token":token, "id":"9T56NNzHE7A","rating":"like"], encoding: URLEncoding.default, headers: nil).responseJSON { (response) in
              print("\(response)")
          }
  </code></pre>
  
  <p>Now because this requires authentication, the API needs an access token. So (as you can see above) I give the API the access token Google Sign-In gives me. Which is: </p>
  
  <pre><code>let token = GIDSignIn.sharedInstance().currentUser.authentication.accessToken!
  </code></pre>
  
  <p>But, the request does not work. The response I get is </p>
  
  <pre><code>SUCCESS: {
      error =     {
          code = 401;
          errors =         (
                          {
                  domain = global;
                  location = Authorization;
                  locationType = header;
                  message = "Invalid Credentials";
                  reason = authError;
              }
          );
          message = "Invalid Credentials";
      };
  }
  </code></pre>
  
  <p>I don't understand how the credentials could be invalid if the user signed in only seconds before. If it helps, here is an example of what my access token looks like (don't worry this isn't actually it):</p>
  
  <blockquote>
    <p>ve35.LmaIBHsK3dUPNR34rb0fgThwiZj-dNB7k935EhyVK1X8nkgMBmA-_3Hxhys7uk-HEm3ggg-HIgJv83RHXhGNKdVkWn0sEn7XtaWhTbeVjg8hsBK3hK8H1Gx8KzhhaEJGVg</p>
  </blockquote>
  
  <p>I've been looking all over the internet trying to find an answer to this but to no avail. So I decided to ask. Does anyone have any idea on how to fix this? Any input would be highly appreciated!</p>