簡體   English   中英

使用OAuth 2.0進行Java和Google Spreadsheets API授權

[英]Java and Google Spreadsheets API Authorization with OAuth 2.0

我想使用Java閱讀Google Spreadsheets,推薦的方法是使用Google Spreadsheets API

當您想要使程序安全時,問題就開始了,因此他們鼓勵您使用OAuth 2.0。 官方頁面中,他們展示了如何僅使用.NET執行此操作,並說“ Java客戶端庫當前不支持OAuth 2.0 ”,並且他們提供了使用OAuth 1.0或使用直接電子郵件密碼進行 Client Login等替代方案。

這是肯定的嗎?,沒有辦法通過Java進行OAuth 2.0身份驗證,也許不是直接使用Java客戶端庫,而是通過具有特定參數的請求。

請相信任何建議。

我還發現, 開發人員文檔為除OAuth2之外的所有內容提供了Java示例,這非常愚蠢。 這是我用來使它工作的一些示例代碼。 為了完整起見,它包括后面部分中的檢索電子表格示例 另請注意,您必須將所需范圍添加到Java DrEdit示例,如下所示。

public class GSpreadsheets {

    private static final String CLIENT_ID = "YOUR_CLIENT_ID";
    private static final String CLIENT_SECRET = "YOUR_SECRET_ID";
    private static final String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

    public static void main(String[] args) throws Exception {

        if (CLIENT_ID.equals("YOUR_CLIENT_ID") || CLIENT_SECRET.equals("YOUR_SECRET_ID")) {
            throw new RuntimeException(
                    "TODO: Get client ID and SECRET from https://cloud.google.com/console");
        }

            // get credentials similar to Java DrEdit example
            // https://developers.google.com/drive/examples/java
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
                httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
                Arrays.asList(DriveScopes.DRIVE, 
                              "https://spreadsheets.google.com/feeds", 
                              "https://docs.google.com/feeds"))
                .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 the service and pass it the credentials you created earlier
        SpreadsheetService service = new SpreadsheetService("MyAppNameHere");
        service.setOAuth2Credentials(credential);

        // Define the URL to request.  This should never change.
        URL SPREADSHEET_FEED_URL = new URL(
            "https://spreadsheets.google.com/feeds/spreadsheets/private/full");

        // Make a request to the API and get all spreadsheets.
        SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
        List<SpreadsheetEntry> spreadsheets = feed.getEntries();

        // Iterate through all of the spreadsheets returned
        for (SpreadsheetEntry spreadsheet : spreadsheets) {
          // Print the title of this spreadsheet to the screen
          System.out.println(spreadsheet.getTitle().getPlainText());
        }
    }
}

Google Data Java Client Library現在支持OAuth 2.0:

https://code.google.com/p/gdata-java-client/source/detail?r=505

不幸的是,庫中沒有完整的樣本顯示如何使用它。 我建議檢查這兩個鏈接,將信息放在一起使其工作:

[編輯]

Java OAuth2代碼

博客帖子在[google-spreadsheet-api]和OAuth2上,帶有代碼
http://soatutorials.blogspot.co.at/2013/08/google-spreadsheet-api-connecting-with.html

相關問題: 使用google gdata客戶端API從Java / Scala獲得OAuth2授權

[結束編輯]

我使用過:Google驅動程序DrEdit教程,完整示例演示了如何將OAuth 2.0與Drive配合使用。 該代碼適用於Google電子表格GData樣式API。 (注意:不包括刷新令牌,但刷新令牌的工作方式正如您所期望的那樣,所以不要太難添加。) -

額外注意:更好的文檔API是Google-Apps-Script。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM