douxuexiao1748 2019-05-30 08:17
浏览 111
已采纳

Google照片返回“错误400:请求包含无效的媒体项目ID。 (INVALID_ARGUMENT)”将媒体项目添加到现有相册中

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.

  • 写回答

1条回答 默认 最新

  • douqi2571 2019-07-05 06:27
    关注

    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)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?