package com.google.api.client.sample.docs.v3;

import com.google.api.client.auth.oauth.OAuthCredentialsResponse;
import com.google.api.client.auth.oauth.OAuthHmacSigner;
import com.google.api.client.auth.oauth.OAuthParameters;
import com.google.api.client.googleapis.auth.authsub.AuthSubHelper;
import 
com.google.api.client.googleapis.auth.oauth.GoogleOAuthAuthorizeTemporaryTokenUrl;
import 
com.google.api.client.googleapis.auth.oauth.GoogleOAuthGetAccessToken;
import 
com.google.api.client.googleapis.auth.oauth.GoogleOAuthGetTemporaryToken;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.javanet.NetHttpTransport;
import com.google.api.client.sample.docs.v3.model.DocsUrl;


import java.awt.Desktop;
import java.awt.Desktop.Action;
import java.net.URI;


public class Auth {

  private static final String APP_NAME ="Google Documents List Data API Java 
Client Sample";

  private static OAuthHmacSigner signer;

  private static OAuthCredentialsResponse credentials;

  static void authorize(HttpTransport transport) throws Exception {
    // callback server
    LoginCallbackServer callbackServer = null;
    String verifier = null;
    String tempToken = null;
    try {
       
      callbackServer = new LoginCallbackServer();
         callbackServer.start();
           // temporary token
      GoogleOAuthGetTemporaryToken temporaryToken =new 
GoogleOAuthGetTemporaryToken();
          signer = new OAuthHmacSigner();
         signer.clientSharedSecret = "anonymous";
          temporaryToken.signer = signer;
      temporaryToken.consumerKey = "anonymous";
      temporaryToken.scope ="https://apps-apis.google.com/a/feeds/user/2.0";;
          temporaryToken.displayName = APP_NAME;
        temporaryToken.callback = callbackServer.getCallbackUrl();
      System.out.println("temporaryToken.callback: 
"+temporaryToken.callback);
      OAuthCredentialsResponse tempCredentials = temporaryToken.execute();
          signer.tokenSharedSecret = tempCredentials.tokenSecret;
          // authorization URL
      GoogleOAuthAuthorizeTemporaryTokenUrl authorizeUrl =
          new GoogleOAuthAuthorizeTemporaryTokenUrl();
           authorizeUrl.temporaryToken = tempToken = tempCredentials.token;
      String authorizationUrl = authorizeUrl.build();
      System.out.println("Go to this authorizationUrl: " + 
authorizationUrl);
      // launch in browser
      boolean browsed = false;
         if (Desktop.isDesktopSupported()) {
         Desktop desktop = Desktop.getDesktop();
        if (desktop.isSupported(Action.BROWSE)) {
          desktop.browse(URI.create(authorizationUrl));
          browsed = true;
        }
      }
      if (!browsed) {
        String browser = "google-chrome";
            Runtime.getRuntime().exec(new String[] {browser, 
authorizationUrl});
      }
      verifier = callbackServer.waitForVerifier(tempToken);
       } finally {
      if (callbackServer != null) {
        callbackServer.stop();
      }
    }
    GoogleOAuthGetAccessToken accessToken = new GoogleOAuthGetAccessToken();
    accessToken.temporaryToken = tempToken;
    accessToken.signer = signer;
    accessToken.consumerKey = "in.gappsdemo.in";
    accessToken.verifier = verifier;
    credentials = accessToken.execute();
    signer.tokenSharedSecret = credentials.tokenSecret;
    createOAuthParameters().signRequestsUsingAuthorizationHeader(transport);
  }

  static void revoke() {
    if (credentials != null) {
      try {
        
GoogleOAuthGetAccessToken.revokeAccessToken(createOAuthParameters());
      } catch (Exception e) {
        e.printStackTrace(System.err);
      }
    }
  }

  private static OAuthParameters createOAuthParameters() {
    OAuthParameters authorizer = new OAuthParameters();
    authorizer.consumerKey = "anonymous";
    authorizer.signer = signer;
    authorizer.token = credentials.token;
    return authorizer;
  }
}




when i am using this code i am getting this error:


Invalid scope: https://apps-apis.google.com/a/feeds/user/2.0

com.google.api.client.http.HttpResponseException: 400 Bad Request
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:209)
    at 
com.google.api.client.auth.oauth.AbstractOAuthGetToken.execute(AbstractOAuthGetToken.java:64)
    at com.google.api.client.sample.docs.v3.Auth.authorize(Auth.java:75)
    at 
com.google.api.client.sample.docs.v3.DocsSample.authorize(DocsSample.java:90)
    at 
com.google.api.client.sample.docs.v3.DocsSample.main(DocsSample.java:49)

-- 
You received this message because you are subscribed to the Google Groups 
"Google Apps Domain Information and Management APIs" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/google-apps-mgmt-apis/-/Gu-7WgTR2u0J.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-apps-mgmt-apis?hl=en.

Reply via email to