Google Photo returns "error 400: Request contains an invalid media item id. (INVALID_ARGUMENT)" when adding a media item into an existing album
Asked Answered
S

1

3

After getting a list of media items and a list of albums from the Google Photo API (using Go and the Google Photo REST API), adding items to an album returns an error.

(Note: using the web interface to add the items to the album works fine).

Code to add the media item to the album:

func (a Album) AddItems(items ...MediaItem) error {
    rel := &url.URL{Path: fmt.Sprintf("/v1/albums/%s:batchAddMediaItems", a.ID)}
    u := a.service.baseURL.ResolveReference(rel)
    for len(items) > 0 {
        ids := []string{}
        for i := 0; i < 50 && i < len(items); i++ {
            ids = append(ids, items[i].ID)
        }
        items = items[len(ids):]
        toAdd := map[string]interface{}{
            "mediaItemIds": ids,
        }
        bodyData, err := json.Marshal(toAdd)
        if err != nil {
            return err
        }
        req, err := http.NewRequest("POST", u.String(), bytes.NewBuffer(bodyData))
        if err != nil {
            return err
        }
        resp, err := a.service.client.Do(req)
        if err != nil {
            return err
        }
        defer resp.Body.Close()
        respErr := struct {
            Error ServerError `json:"error"`
        }{}
        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            return err
        }
        err = json.Unmarshal(body, &respErr)
        if err != nil {
            return err
        }
        err = respErr.Error.Error()
        if err != nil {
            return err
        }
    }
    return nil
}

The server returns the following error:

error 400: Request contains an invalid media item id. (INVALID_ARGUMENT). 

The media item ID is copied from the ID field of the JSON representation of a media item returned from a search request. The other fields of the media item seem to be valid (e.g. the ProductURL).

What is wrong in this batchAddMediaItems request? or how to get a valid media item ID suitable for batchAddMediaItems ?

Thank you.

Sewell answered 30/5, 2019 at 8:17 Comment(0)
B
13

According to the guide (https://developers.google.com/photos/library/guides/manage-albums), unfortunately, you can only add media items that have been uploaded by your application to albums that your application has created. Media items must also be in the user's library.

It was the feature request on issuetracker for the organising existing mediaitems (https://issuetracker.google.com/issues/109505022), but developers allow only working with app's photo/album. But now it exist the new feature request about exactly our problem, so lets star it together (https://issuetracker.google.com/issues/132274769)

Berrios answered 5/7, 2019 at 6:27 Comment(4)
Just making it very clear as I missed reading it a few times --> You can only add mediaItems you create/upload to albums you create. What a pain :(Yulma
You're right, just quoted them. And yes, it's kinda sad.Berrios
@SanJay And when you say you, the actual meaning is "your application". Correct?Chorizo
Yes I mean applicationYulma

© 2022 - 2024 — McMap. All rights reserved.