title: Google Rest API: "Invalid JWT: Failed Audience Check"? I am using audience field API suggests
body: <p>I am attempting to connect to Google's REST API (for Google Calendars) using a JWT. I have followed the instructions up to and including <a href="https://developers.google.com/identity/protocols/OAuth2ServiceAccount#authorizingrequests" rel="nofollow noreferrer">here</a>. When I send a post request with my JWT (with <code>grant_type</code> and <code>assertion</code> in the body per previous StackOverflow posts), I get an error:</p>
  
  <pre><code>{
    "error": "invalid_grant",
    "error_description": "Invalid JWT: Failed audience check. The right audience is https://www.googleapis.com/oauth2/v4/token"
  }
  </code></pre>
  
  <p>My code is short so I'll post it in full (sans private details) below. I'm using the <code>jaguar_jwt</code> library and modeled my JWT generation off of their sample code. I have create a service account and I believe followed all instructions up until the aforementioned point in Google's documentation.</p>
  
  <pre><code>import 'package:http/http.dart' as http;
  import 'package:jaguar_jwt/jaguar_jwt.dart';
  
  String generateJWT() {
    final String privateKey = "-----BEGIN PRIVATE KEY-----.********=\n-----END PRIVATE KEY-----\n";
  
    final claimSet = new JwtClaim(
        issuer: 'google-api@**********.iam.gserviceaccount.com',
        audience: &lt;String&gt;['https://www.googleapis.com/oauth2/v4/token'],
        otherClaims: &lt;String,dynamic&gt;{
          "scope":"https://www.googleapis.com/auth/calendar",
          "access_type": "offline"
        },
        maxAge: const Duration(minutes: 60));
  
    String token = issueJwtHS256(claimSet, privateKey);
    // print(token);
  
    return token;
  }
  
  void sendJWT(String jwt) async {
    var client = new http.Client();
    try {
      String googleUri = "https://www.googleapis.com/oauth2/v4/token";
  
      var requestBody = {'grant_type':'urn:ietf:params:oauth:grant-type:jwt-bearer',
                          'assertion':'$jwt',
                          "access_type": "offline"
                        };
  
      await client.post(googleUri, body: requestBody)
            .then((value) =&gt; print(value.body));
  
    } finally {
      client.close();
    }
  }
  
  main(List&lt;String&gt; arguments) {
    sendJWT(generateJWT());
  }
  
  </code></pre>