0
votes

I created a spreadsheet using Google Sheets API with service account however I can't reach it when I try to reach with url I get an access error. I also tried set permissions.create but I couldn't figured out how can I find file(when I use id of spreadsheet which is in url, I take file not found error on Google Drive API) id that contain this spreadsheet. Is there any way to make spreadsheets public (maybe when creating)?

Here is my createspreadsheet function

func createSpreadsheet(ctx context.Context, title string, creds []byte) (*GoogleSpreadsheet, error) {

    srv, err := sheets.NewService(ctx, option.WithCredentialsJSON(creds))
    if err != nil {
        return nil, err
    }

    var (
        prop = &sheets.SpreadsheetProperties{
            Title: title,
        }

        sheet = &sheets.Spreadsheet{
            Properties: prop,
        }
    )
    resp, err := srv.Spreadsheets.Create(sheet).Do() //Create request is send
    if err != nil {
        return nil, err
    }
    gs := &GoogleSpreadsheet{
        SpreadSheetID:   resp.SpreadsheetId,
        SpreadSheetName: title,
        SheetID:         resp.Sheets[0].Properties.SheetId,
        SheetName:       resp.Sheets[0].Properties.Title,
        URL:             resp.SpreadsheetUrl,
        Service:         srv,
    }

    mux.Lock()
    defer mux.Unlock()
    spreadsheets[gs.SpreadSheetID] = gs
    return gs, nil
}
1
From however I can't reach it when I try to reach with url I get an access error, I cannot understand about the detail situation. I apologize for this. Can I ask you about the detail of it?Tanaike
Please consider providing the relevant parts of the code you are working on. When you call permissions.create, you do it with the same account that originally created the spreadsheet?Iamblichus
@Tanaike Of course I can :) ,I found spreadsheet url which is created by service account from my code and I tried to reach to spreadsheet with this url (docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxx/edit) and I get an error that says you need to access.pangea
@Iamblichus You are right I apologize for this, I added my function and the answer of your question is I did it with my main gmail account which is parent of my service account I dont know how to do it with service account. I couldn't understand clearly so if I can take help it would be greate.pangea
Thank you for replying. About I found spreadsheet url which is created by service account from my code and I tried to reach to spreadsheet with this url (docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxx/edit) and I get an error that says you need to access., in this case, how do you want to access to the Spreadsheet?Tanaike

1 Answers

0
votes

Issue:

You created the spreadsheet with one account and tried to retrieve it with another account (one being your regular account and the other a service account).

It's normal that the file is not found, since it is not shared with the other account. The service account is not you, so creating a file with your regular account doesn't give access to your service account, and vice versa.

Solution:

Call Permissions: create with the same account with which you created the spreadsheet if you want other accounts to have access to it.

For example, if you wanted to make it public, the request body would be:

{
  "role": "reader", // Or "writer"? Depends on what you want
  "type": "anyone"
}

Or, if you wanted to share with a certain account, it would be:

{
  "role": "reader", // Or "writer"? Depends on what you want 
  "type": "user",
  "emailAddress": "your_email_address"
}