googleapis / google-api-go-client

Auto-generated Google APIs for Go.
https://pkg.go.dev/google.golang.org/api
BSD 3-Clause "New" or "Revised" License
4.03k stars 1.13k forks source link

Unable to publish custom app. #2097

Closed Mofiqul closed 1 year ago

Mofiqul commented 1 year ago

I have tried the following code.

const (
    packageName = "com.example.app"
)

func PublishApp(bundlePath string) {
    ctx := context.Background()

    content, err := ioutil.ReadFile("path/to/service-account.json"))
    if err != nil {
        log.Printf("Failed to read service account file: %v", err)
        return err
    }

    creds, err := google.CredentialsFromJSON(ctx, content, playcustomapp.AndroidpublisherScope)
    if err != nil {
        log.Printf("Failed to create credentials: %v", err)
        return err
    }

    svc, err := playcustomapp.NewService(ctx, option.WithCredentials(creds))
    if err != nil {
        log.Printf("Failed to create Custom App Service: %v", err)
        return err
    }

    app := &playcustomapp.CustomApp{
        PackageName:  packageName,
        Title:        "My Custom App",
        LanguageCode: "en-Us",
    }

    bundleFile, err := os.Open(bundlePath)
    if err != nil {
        log.Printf("Failed to open bundle file: %v", err)
        return err
    }

        // Error happening here
    res, err := svc.Accounts.CustomApps.Create(123456, app).Media(bundleFile).Do() // used correct developer id

    if err != nil {
        log.Printf("Failed to publish app: %v", err)
        return err
    }

    log.Print(res)
}

Received Error:

Error 401: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project., unauthorized

I have created the service account and granted permission as documented. Can someone help me with what am I doing wrong.

codyoss commented 1 year ago

At a glance, the code looks correct to me. That error usually occurs if there is no credential provided. Try to manually pull a token to make sure there is not issues going on with that:

tok, err := creds.TokenSource.Token()
if err != nil {
   log.Fatal(err)
}
Mofiqul commented 1 year ago

@codyoss - I tried the your code and got the token as expected. So I have changed my code to

    svc, err := playcustomapp.NewService(ctx, option.WithTokenSource(creds.TokenSource))
    if err != nil {
        log.Printf("Failed to create Custom App Service: %v", err)
        return err
    }

But still having the same issue.

codyoss commented 1 year ago

Hmmm I am not sure in that case. Have you followed all of the step on https://developers.google.com/android/work/play/custom-app-api/get-started ? Whatever the issue, I believe it to be unique to this api and/or the play apis in general. I am more familiar with the cloud apis so I am not a great help here. You may have better luck reaching out and asking for support from the play side of things -- all of these libraries are autogenerated based on a spec and I don't believe this is an issue with the library itself, but rather something with the credential that is being used in this instance.

codyoss commented 1 year ago

The only other suggestion I would have to try is you could try using an apikey instead with https://pkg.go.dev/google.golang.org/api/option#WithAPIKey

Mofiqul commented 1 year ago

@codyoss - I've already attempted using the API Key approach, but unfortunately, I haven't had any success with that method either.

Mofiqul commented 1 year ago

Hmmm I am not sure in that case. Have you followed all of the step on https://developers.google.com/android/work/play/custom-app-api/get-started ? Whatever the issue, I believe it to be unique to this api and/or the play apis in general. I am more familiar with the cloud apis so I am not a great help here. You may have better luck reaching out and asking for support from the play side of things -- all of these libraries are autogenerated based on a spec and I don't believe this is an issue with the library itself, but rather something with the credential that is being used in this instance.

Yes. I did everything according to that documentation.

codyoss commented 1 year ago

I am not sure than. If you have a support contract I suggest opening up a ticket and/or you may have better luck through the play/android support channels of communication.

harsh-im commented 1 year ago

@Mofiqul Hello, I am facing the same issue while uploading app bundle using https://developers.google.com/android-publisher/api-ref/rest/v3/edits.bundles/upload API, in my case I am able to hit https://developers.google.com/android-publisher/api-ref/rest/v3/edits/insert API successfully. have you able to resolve your issue ?

Edit: I have tried option.WithTokenSource(creds.TokenSource) but I am seeing a very strange and random behaviour. It is sometimes working and most of the time giving the same error.

any help will be appreciated. @codyoss @Mofiqul

harsh-im commented 1 year ago

@codyoss @Mofiqul @entombedvirus any update??

codyoss commented 1 year ago

My advice is the same as my last comment on this issue: "If you have a support contract I suggest opening up a ticket and/or you may have better luck through the play/android support channels of communication." I don't believe there is an issue on the generated client side, but rather this is a usability of the service/ setup issue.

harsh-im commented 1 year ago

@Mofiqul have you resolved it?

Mofiqul commented 1 year ago

@harsh-im - My problem was playcustomapp not androidpublisher.

const (
    packageName = "com.example.app"
)
func Publish(bundlePath string) error {
    ctx := context.Background()

    data, err := ioutil.ReadFile("path/to/service-account.json"))
    if err != nil {
        log.Fatalf("Failed to read service account key: %v", err)
    }

    creds, err := google.CredentialsFromJSON(ctx, data, androidpublisher.AndroidpublisherScope)
    if err != nil {
        log.Fatalf("Failed to create credentials: %v", err)
    }

    svc, err := androidpublisher.NewService(ctx, option.WithCredentials(creds))
    if err != nil {
        log.Fatalf("Failed to create AndroidPublisher service: %v", err)
    }

    editResponse, err := svc.Edits.Insert(packageName, nil).Do()
    if err != nil {
        log.Fatalf("Failed to create edit: %v", err)
    }
    fmt.Printf("Edit created successfully. Edit ID: %s\n", editResponse.Id)

    bundleFile, err := os.Open(bundlePath) // Replace with the APK or App Bundle file path
    uploadResponse, err := svc.Edits.Apks.Upload(packageName, editResponse.Id).Media(bundleFile).Do()

    if err != nil {
        log.Fatalf("Failed to upload APK/App Bundle: %v", err)
    }
    fmt.Printf("APK/App Bundle uploaded successfully. Version code: %d\n", uploadResponse.VersionCode)

    _, err = svc.Edits.Commit(packageName, editResponse.Id).Do()
    if err != nil {
        log.Fatalf("Failed to commit changes: %v", err)
    }
    fmt.Println("Changes committed successfully. App published!")
    return nil
}

This is how I made it work for me, but the downside is that you have to publish the app manually at least once.

Check the expo doc here

harsh-im commented 1 year ago

@Mofiqul @codyoss Thank you for the reply. I was doing the same as you have suggested but still I was getting 401 error (sometimes it was working as I mentioned previously) as a workaround I added a 1 minute sleep timer after the Insert Edit API & then it was working fine.

srinuu7878 commented 11 months ago

i am seeing the same issue Error 401: Request is missing required authentication credential. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project., unauthorized can anyone help us to fix this issue tried all possible ways