title: "Login Required" 401 Unauthorized message when calling the v3 Google Calendar API using a Service Account via OAuth 2.0
body: <p>First, let me explain what I am trying to do, as this is a two part question.</p>
  
  <p>I am building a JAX-RS service that internally authenticates with a Google account via OAuth2, so it can access and manipulate a Google calendar. This service will be called by a web site (Joomla), which will allow users that login into the site to register their email address with calendar events so they can get email notifications when the events are near. These users have no knowledge of the underlying Google account.</p>
  
  <p>So my first question, is using Service Account authentication the right thing? Looking at the Google docs, its the only use case that caters for non-human authentication (so server to server authentication).</p>
  
  <p>The second problem is that I have written some code to do this, but I get back a 401/Unauthorized response back the following error back when I call the execute method the Calendar feed. This code was lifted from the Google samples.</p>
  
  <p>I am using v3-rev3-1.5.0-beta of the the Google Calendar API. I have turned on the Calendar API in the account, and created and downloaded a private key, that is used in the code below.</p>
  
  <p>I am calling the code below in Eclipse locally, not from a web server.</p>
  
  <p>So the first call I make is to get back a credential object (Id's obfuscated with X's):</p>
  
  <pre><code>private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
  private static final JsonFactory JSON_FACTORY = new JacksonFactory();
  .
  .
  .
  GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
              .setJsonFactory(JSON_FACTORY)
              .setServiceAccountId("284XXXXXXXX@developer.gserviceaccount.com")
              .setServiceAccountScopes(CalendarScopes.CALENDAR)
              .setServiceAccountPrivateKeyFromP12File(new File("D:/3cd8XXXXXXXXXXXXXXXXXXXXXXXXXXXXXd635e-privatekey.p12"))
              .build();
  </code></pre>
  
  <p>The next step is to then call the Calendar API, like so (no other code called in between):</p>
  
  <pre><code> Calendar calendar = Calendar.builder(HTTP_TRANSPORT, JSON_FACTORY)
          .setApplicationName("TestApp-Calendar/1.0").setHttpRequestInitializer(credential)
          .build();
  
   CalendarList feed = calendar.calendarList().list().execute();
  </code></pre>
  
  <p>The call to list().execute() results in the following error:</p>
  
  <blockquote>
    <p>com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
    {
      "code" : 401,
      "errors" : [ {
        "domain" : "global",
        "location" : "Authorization",
        "locationType" : "header",
        "message" : "Login Required",
        "reason" : "required"
      } ],
      "message" : "Login Required"
    }
        at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:159)
        at com.google.api.client.googleapis.json.GoogleJsonResponseException.execute(GoogleJsonResponseException.java:187)
        at com.google.api.client.googleapis.services.GoogleClient.executeUnparsed(GoogleClient.java:115)
        at com.google.api.client.http.json.JsonHttpRequest.executeUnparsed(JsonHttpRequest.java:112)
        at com.google.api.services.calendar.Calendar$CalendarList$List.execute(Calendar.java:510)
        at uk.org.reigatepriorybowmen.Service.registerEmailForAllCalendarEvents(Service.java:55)
        at uk.org.reigatepriorybowmen.Service.main(Service.java:74)</p>
  </blockquote>
  
  <p>The service account is setup as follows:</p>
  
  <p><img src="https://i.stack.imgur.com/RbhVj.jpg" alt="enter image description here"></p>
  
  <p>So, what am I doing wrong?! I have spent hours Googling around looking for solutions so this is my last hope.</p>
  
  <p>Many thanks in advance for your help.</p>
  
  <p>Justin</p>