error 400 Bad Request in Google photos api 'share album'

26 views Asked by At

I'm working on a web app, and I've encountered an issue when trying to share an album with my server side. Creating an album for a user works fine, but sharing it results in a 400 error. In my OAuth consent screen settings, I have included the scope Google APIs Auth photoslibrary.sharing, and I am currently in Test mode. Additionally, I have added the email of the test user to the 'Test users' list.

Here is the code snippet:

private async Task ShareAlbum(PhotosLibraryService userPhotosService, string albumId) {
  // fetch the file Service_Account.json
  string serviceAccountJsonPath = Path.Combine(Directory.GetCurrentDirectory(), "Service_Account.json");
  string serviceAccountJson = System.IO.File.ReadAllText(serviceAccountJsonPath);

  var serviceAccountCredential = GoogleCredential.FromJson(serviceAccountJson)
    .CreateScoped(PhotosLibraryService.Scope.Photoslibrary, "https://www.googleapis.com/auth/photoslibrary.sharing");

  var serviceAccountPhotosService = new PhotosLibraryService(new BaseClientService.Initializer {
    HttpClientInitializer = serviceAccountCredential
  });

  // Specify the sharing options
  var sharedAlbumOptions = new SharedAlbumOptions {
    IsCollaborative = true, // Set this based on your requirement
      IsCommentable = false // You can adjust this option as needed
  };

  var shareInfo = new ShareInfo {
    SharedAlbumOptions = sharedAlbumOptions
  };

  var shareRequest = new {
    shareInfo = shareInfo
  };
  var requestUrl = $ "https://photoslibrary.googleapis.com/v1/albums/{albumId}:share";

  var httpRequest = new HttpRequestMessage(HttpMethod.Post, requestUrl) {
    Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(shareRequest), Encoding.UTF8, "application/json")
  };
  // Modified lines end here

  try {
    var response = await serviceAccountPhotosService.HttpClient.SendAsync(httpRequest);
    response.EnsureSuccessStatusCode();

    Console.WriteLine($"Album shared successfully with server-side account. Response: {await response.Content.ReadAsStringAsync()}");
  } catch (HttpRequestException ex) {
    Console.WriteLine($"Error sharing album with server-side account: {ex.Message}");

    // Log more details about the exception
    if (ex.StatusCode != null) {
      Console.WriteLine($"HTTP Status Code: {ex.StatusCode}");
    }
  }
}

I'm unsure if this issue relates to the scopes, or if there's a problem with the content of my HTTP request.

0

There are 0 answers