I'm trying to get message information from the Google API using a service account and a JWT.
I've already enabled Domain wide delegation. I should probably mention that I'm not a GSuite admin, just a user who is trying to get messages from my own Inbox using a Go script.
func main() {
data, err := ioutil.ReadFile("JWT_Credentials.json")
conf, err := google.JWTConfigFromJSON(data, gmail.GmailReadonlyScope)
client := conf.Client(oauth2.NoContext)
// Create a new gmail service using the client
gmailService, err := gmail.New(client)
fullMessage, err := gmailService.Users.Messages.Get("me", message.Id).Format("metadata").Do()
if err != nil {
log.Printf("Error: %v", err)
}
}
and my JWT credentials are as follows:
{
"type": "service_account",
"project_id": "PROJECT_ID",
"private_key_id": "PRIVATE_KEY_ID",
"private_key": "-----BEGIN PRIVATE KEY-----\PRIVATE_KEY",
"client_email": "SERVICE@PROJECT_ID.iam.gserviceaccount.com",
"client_id": "CLIENT_ID",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url":
"https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "CERT_URL"
}
I get the following error when the code runs:
googleapi: Error 400: Bad Request, failedPrecondition
If anybody has the solution to this problem or has encountered it in the past, please help me out!