0

I'm getting the error below while trying to upload media to Google Photos API, following the docs

Google Photos API Error

This is how i retrieve my bytes array:

Retrieve bytes array

And this is how i make the request:

enter image description here

I've tried a lot of things and none of it work...

Note: I'm consuming other Google Photos API endpoints, such as Get Albums, Create Albums, Get Media and everything work as expected. The upload media is the only one i'm having trouble with.

Note 2: The token is being sent correctly.

Note 3: All the origin endpoints were configured in the google console (localhost included) so much so that other endpoints are working correctly.

Anyone can give me a light?

Mateus Cerqueira
  • 457
  • 4
  • 13

1 Answers1

0

I am writing something similar in C# and was able to work with the photo api. My best advice is to double check headers. I hope this helps, additionally I added a postman screenshot of a successful call to the api:

public async Task<GoogleApiResponse> UploadPhoto(StorageFile photo)
        {
            var userInfo = UserInfoVault.GetUserInfo();
            HttpClient client = new HttpClient();
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", userInfo.AccessToken);
            client.DefaultRequestHeaders.Add("X-Goog-Upload-File-Name", photo.DisplayName);

            var fileStream = await photo.OpenAsync(FileAccessMode.Read);
            var reader = new DataReader(fileStream.GetInputStreamAt(0));
            await reader.LoadAsync((uint)fileStream.Size);
            byte[] pixels = new byte[fileStream.Size];
            reader.ReadBytes(pixels);

            var httpContent = new ByteArrayContent(pixels);
            httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");

            HttpResponseMessage photoResponse = client.PostAsync(ApiEdnpoints.UploadPhoto, httpContent).Result;
            string strResponse = await photoResponse.Content.ReadAsStringAsync();

            return null;
        }

Postman screenshot of successful call to upload a photo