douxuexiao1748
douxuexiao1748
2019-05-30 08:17

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

  • rest
已采纳

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条回答