gpt4 book ai didi

c# - 谷歌相册 API mediaItems :batchCreate : NOT_IMAGE: There was an error while trying to create this media item

转载 作者:行者123 更新时间:2023-11-30 17:28:35 27 4
gpt4 key购买 nike

我正在尝试使用 C# 将图像文件上传到照片库。

使用范围: https://www.googleapis.com/auth/photoslibrary https://www.googleapis.com/auth/photoslibrary.appendonly

我正在使用以下方法生成 uploadToken:

FileStream fS = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
byte[] b = new byte[fS.Length];
fS.Read(b, 0, (int)fS.Length);
fS.Close();
var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://photoslibrary.googleapis.com/v1/uploads");
httpWebRequest.ContentType = "application/octet-stream";

httpWebRequest.Headers.Add("Authorization:" + "Bearer " + auth_Token);
httpWebRequest.Headers.Add("X-Goog-Upload-File-Name:" + imageName);
httpWebRequest.Headers.Add("X-Goog-Upload-Protocol:" + "raw");
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
streamWriter.Write(b);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

在这里,我成功获取了 uploadToken。在此之后,我继续创建 mediaItem,如下所示:

 httpWebRequest = (HttpWebRequest)WebRequest.Create("https://photoslibrary.googleapis.com/v1/mediaItems:batchCreate");
httpWebRequest.ContentType = "application/json";
httpWebRequest.Headers.Add("Authorization:" + "Bearer " + auth_Token);
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string albumID = "someid";
string itemDesc = "somedesc";
string jsonString = "{\"albumId\": \"" + albumID + "\",\"newMediaItems\": [{ \"description\": \"" + itemDesc + "\", \"simpleMediaItem\": { \"uploadToken\": \"" + uploadToken + "\" }}]}";

streamWriter.Write(jsonString);
streamWriter.Flush();
streamWriter.Close();
}
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

现在我得到的回应是:

status": { "code": 3, "message": "NOT_IMAGE: There was an error while trying to create this media item." }

我在谷歌上搜索了很多,但没有找到任何解决这个问题的方法,也没有找到使用 Google Photos Library API 的 mediaUpload 的任何代码块。

请告诉我哪里做错了。

-以错误的方式读取/写入文件字节?

-使用Json字符串写错了?

请帮帮我。谢谢。

最佳答案

在上传步骤中将图像写入正文时,我通过执行以下操作成功完成了这项工作。希望这对您有所帮助!

 using (var stream = httpWebRequest.GetRequestStream())
{
stream.Write(b, 0, b.Length);
stream.Flush();
stream.Close();
}

关于c# - 谷歌相册 API mediaItems :batchCreate : NOT_IMAGE: There was an error while trying to create this media item,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52645488/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com