2016-11-07 8 views
0

Spring MVCで書かれたバックエンドからGoogleスプレッドシートにアクセスしようとしています。これは「作業」が、されてhttps://developers.google.com/sheets/quickstart/javaSpring MVCからGoogleスプレッドシートにアクセス

アプリケーションの出力はリンクを私に提供:

public static Sheets getSheetsService() throws IOException { 
    return new Sheets.Builder(HTTP_TRANSPORT, JSON_FACTORY, authorize()) 
      .setApplicationName(APPLICATION_NAME) 
      .build(); 
} 

public static Credential authorize() throws IOException { 
    // Load client secrets. 
    InputStream in = 
      ConcreteSheetsController.class.getResourceAsStream("/secret.json"); 
    GoogleClientSecrets clientSecrets = 
      GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in)); 

    // Build flow and trigger user authorization request. 
    GoogleAuthorizationCodeFlow flow = 
      new GoogleAuthorizationCodeFlow.Builder(
        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES) 
        .setDataStoreFactory(DATA_STORE_FACTORY) 
        .setAccessType("offline") 
        .build(); 
    Credential credential = new AuthorizationCodeInstalledApp(
      flow, new LocalServerReceiver()).authorize("user"); 
    System.out.println(
      "Credentials saved to " + DATA_STORE_DIR.getAbsolutePath()); 
    return credential; 
} 

作成された秘密は、それがここで説明されている方法である:チュートリアルを使用して私はこのような何かを得ます。私がそれをクリックすると、ブラウザは私にGoogleアカウントを選択するように頼みます。私がそうすれば、すべてうまくいくが、私はサーバーにもっと適したソリューションを持っていたいと思う。自分の秘密のjson-fileを使用して、私のアプリケーションが私自身のユーザーアカウントを提供せずにログインできるようにしたいと思います。その仕事をする方法はありますか?

+0

あなたは(https://developers.google.com/identity/protocols/OAuth2ServiceAccount)[サーバアプリケーションへのサーバーのためのOAuth 2.0]使用中のサービスアカウントを見て試すことができます。特に、クラウドAPIを呼び出してユーザー固有のデータではなくプロジェクトベースのデータにアクセスする場合は、API呼び出しを行うことができます。 – noogui

答えて

0

google.v4の代わりにGdataサードパーティのライブラリをお試しください。ここで私が資格情報を渡すために使用するコードです。 P12ファイルgoogle's consoleを作成する必要があります。

明らかに、ワークシート/ワークブックの読み書きのほとんどは、gdataメソッドと一致するように変更する必要があります。

私はこれが助けてくれると願っています。質問があれば、私は助けてくれるかもしれません。

// Application name 
    private static final String APPLICATION_NAME = "spreadsheet-application-name"; 

    // account info and p12 
    private static final String ACCOUNT_P12_ID = "[email protected]"; 
    private static File P12FILE; 

    private static String ssName = "Spreadsheet name"; 
    private static String wsName = "worksheetname"; //this has to be all lower case for some reason 

    // scopes 
    private static final List<String> SCOPES = Arrays.asList(
      "https://docs.google.com/feeds", 
      "https://spreadsheets.google.com/feeds"); 

    // Spreadsheet API URL 
    private static final String SPREADSHEET_URL = "https://spreadsheets.google.com/feeds/spreadsheets/private/full"; 

    private static final URL SPREADSHEET_FEED_URL; 

    static { 
     try { 
      SPREADSHEET_FEED_URL = new URL(SPREADSHEET_URL); 
      P12FILE = getP12File(); 
     } catch (MalformedURLException e) { 
      throw new RuntimeException(e); 
     } 
    } 
    // Authorize- 
    private static Credential authorize() throws Exception { 

     HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
     JsonFactory jsonFactory = new JacksonFactory(); 

     GoogleCredential credential = new GoogleCredential.Builder() 
       .setTransport(httpTransport) 
       .setJsonFactory(jsonFactory) 
       .setServiceAccountId(ACCOUNT_P12_ID) 
       .setServiceAccountPrivateKeyFromP12File(P12FILE) 
       .setServiceAccountScopes(SCOPES) 
       .build(); 

//  boolean ret = credential.refreshToken(); 
     // debug dump 
//  Log.debug("refreshToken:" + ret); 

     // debug dump 
//  if (credential != null) { 
//   Log.debug("AccessToken:" + credential.getAccessToken()); 
//  } 


     return credential; 
    } 

    // Get service 
    private static SpreadsheetService getService() throws Exception { 

     SpreadsheetService service = new SpreadsheetService(APPLICATION_NAME); 
     service.setProtocolVersion(SpreadsheetService.Versions.V3); 

     Credential credential = authorize(); 
     service.setOAuth2Credentials(credential); 

     // debug dump 
//  Log.debug("Schema: " + service.getSchema().toString()); 
//  Log.debug("Protocol: " + service.getProtocolVersion().getVersionString()); 
//  Log.debug("Service Version: " + service.getServiceVersion()); 


     return service; 
    } 

    private static File getP12File(){ 
     ClassLoader classLoader = GoogleSheetsWriter.class.getClassLoader(); 
     return new File(classLoader.getResource("DriveAPITest-bf290e0ee314.p12").getFile()); 
    } 
関連する問題