2

I am using Google Forms API to get form responses using service account.

package com.form.api.poc;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Objects;

import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.services.forms.v1.Forms;
import com.google.api.services.forms.v1.FormsScopes;
import com.google.api.services.forms.v1.model.ListFormResponsesResponse;
import com.google.auth.oauth2.GoogleCredentials;




public class Test1 {
    
    private static final String APPLICATION_NAME = "google-form-api-project";
    private static Forms formsService;

    static {

        try {
            JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
            formsService = new Forms.Builder(GoogleNetHttpTransport.newTrustedTransport(),
                    jsonFactory, null)
                    .setApplicationName(APPLICATION_NAME).build();
            
            
            

        } catch (GeneralSecurityException | IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) throws IOException {
        ListFormResponsesResponse response = readResponses("form_id_jdnfka_123b34$Q%#nknk", getAccessToken());
        System.out.println(response.toPrettyString());
        
    }
    
    
    public static String getAccessToken() throws IOException {
        
        
        GoogleCredentials credential = GoogleCredentials.fromStream(Objects.requireNonNull(
        Test1.class.getResourceAsStream("/serviceaccount_cred.json"))).createScoped(Arrays.asList(FormsScopes.FORMS_BODY_READONLY));
        
        return credential.refreshAccessToken().getTokenValue();
  }
    
    private static ListFormResponsesResponse readResponses(String formId, String token) throws IOException {
    return  formsService.forms().responses().list(formId).setOauthToken(token).execute();
    }
}

But, I am getting following error as a response:

GET https://forms.googleapis.com/v1/forms/form_id_jdnfka_123b34$Q%#nknk/responses?oauth_token=token_id.......... { "code" : 403, "details" : [ { "@type" : "type.googleapis.com/google.rpc.ErrorInfo", "reason" : "ACCESS_TOKEN_SCOPE_INSUFFICIENT" } ], "errors" : [ { "domain" : "global", "message" : "Insufficient Permission", "reason" : "insufficientPermissions" } ], "message" : "Request had insufficient authentication scopes.", "status" : "PERMISSION_DENIED" }

I have followed the documentation provided at the following pages: using-service-account

Delegating-domain wide authority

But, not sure, what is wrong, any help would be appreciated. Thank you in advance!

sridar1992
  • 21
  • 2
  • You have form body read only access in scopes also I have concern about refresh token. Maybe you need to get access token instead of refresh token – Gurkan İlleez Jul 27 '22 at 17:02

1 Answers1

0

Two things has to be done here, first, use FormsScopes .FORMS_RESPONSES_READONLY for responses and the service account email address shall have permission on the given form_id.

Yet, not sure, if its the right way to go ahead, maybe if executed locally thats the reason it requires the permission, maybe if the above program once executed in some GC environment, then the permission need not be given explicitly to the service account on the form.

sridar1992
  • 21
  • 2