Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google.Apis.Requests.RequestError Login Required [401] Message[Login Required] Location

So I downloaded .NET client for Cloud Storage from Nuget (Google.Apis.Storage.v1, version=1.8.1.10), created a project using Developer Console, generated API key, enabled billing, created a bucket.
But when I'm trying to list all objects in a bucket, I'm getting this error (login required). Any ideas why that may be? Cloud Storage and its JSON API are enabled in console.
Here's full code from my sample app: https://gist.github.com/chester89/5d6a62633abab3969c32

like image 915
chester89 Avatar asked May 18 '14 22:05

chester89


1 Answers

An API key is a simple marker that says "this call is associated with this project." It is useful for managing quota, but it is not a secret and does not authenticate the caller as any particular user. Your bucket presumably is not browsable by anonymous callers, and your call is made anonymously.

You could fix this by granting anonymous users read access to your bucket, but a better idea would be to authenticate your requests. You have several options on how to do this, depending on whether you want to make the calls with your own personal credentials or whether you want to use a service account associated with your application. Full instructions are documented here: https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

Guessing that you want to authenticate as yourself and that you're writing an application that will run on your desktop, here's an example:

UserCredential credential;
using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read))
{
   credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(
         GoogleClientSecrets.Load(stream).Secrets,
         new[] { BooksService.Scope.Books },
         "user", CancellationToken.None, new FileDataStore("Books.ListMyLibrary"));
}
// Create the service.
var service = new BooksService(new BaseClientService.Initializer()
{
   HttpClientInitializer = credential,
         ApplicationName = "Books API Sample",
});
like image 148
Brandon Yarbrough Avatar answered Nov 01 '22 15:11

Brandon Yarbrough