5

Really stuck here. The code, built from examples provided by Google:

public static void main(String[] args) { try { HttpTransport httpTransport = new NetHttpTransport(); JsonFactory jsonFactory = new JacksonFactory(); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET, Arrays.asList(DriveScopes.DRIVE)) .setAccessType("online") .setApprovalPrompt("auto") .build(); String url = flow.newAuthorizationUrl().setRedirectUri(REDIRECT_URI).build(); System.out.println("Please open the following URL in your browser then type the authorization code:"); System.out.println(" " + url); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String code = br.readLine(); GoogleTokenResponse response = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute(); GoogleCredential credential = new GoogleCredential().setFromTokenResponse(response); //Create a new authorized API client Drive service = new Drive.Builder(httpTransport, jsonFactory, credential).setApplicationName("Google Push").build(); //Insert a file File body = new File(); body.setTitle("My document"); body.setDescription("A test document"); body.setMimeType("text/plain"); java.io.File fileContent = new java.io.File("document.txt"); BufferedWriter bw = new BufferedWriter(new FileWriter(fileContent)); bw.write("allo!!!!"); bw.close(); System.out.println("file created? -> " + fileContent.createNewFile()); FileContent mediaContent = new FileContent("text/plain", fileContent); File file = service.files().insert(body, mediaContent).execute(); System.out.println("File ID: " + file.getId()); watchChange(service, "channelId", "web_hook", "https://clementlevallois.net/notifications"); // line 78 } catch (IOException ex) { Logger.getLogger(DriveCommandLine.class.getName()).log(Level.SEVERE, null, ex); } } private static Channel watchChange(Drive service, String channelId, String channelType, String channelAddress) { Channel channel = new Channel(); channel.setId(channelId); channel.setType(channelType); channel.setAddress(channelAddress); try { return service.changes().watch(channel).execute(); //line 91 } catch (IOException e) { e.printStackTrace(); } return null; } 

The stack trace:

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113) at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312) at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343) at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460) at Controller.DriveCommandLine.watchChange(DriveCommandLine.java:91) at Controller.DriveCommandLine.main(DriveCommandLine.java:78) 
7
  • Are you behind a proxy server? Commented Mar 6, 2014 at 20:17
  • Not behind a proxy... Commented Mar 7, 2014 at 11:21
  • could you dump the http request and response into your question Commented Mar 11, 2014 at 6:09
  • are you sure your token/key is valid? Commented Mar 12, 2014 at 9:18
  • I'm getting the same error... was this ever resolved ? Commented Jun 18, 2015 at 14:39

3 Answers 3

1

API returning an HTTP 401 or HTTP 403 response when calling the Drive API. These errors could indicate any of:

Token expiry, Token revocation, (This would cause both the access token and the refresh token to stop working), Token not authorized for needed scopes, Request not authorized correctly with OAuth 2.0 protocol.

Token expiry can be handled by calling refreshToken(). If that call fails with an "Invalid Credentials"

Sign up to request clarification or add additional context in comments.

Comments

0

Here is some steps that might help you resolve your problem:

-Look into your Google Apps account maybe your Docs API access is turned off.

-Try verifying your token and key and you redirect url.

Comments

-1

credential.getRequestInitializer() is null check it

Read this post 101% help you

Get Userinfo from Oauth2 Google Contacts API

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.