Google表格API - 获取永久访问令牌

3
我有一个简单的Java程序在我的电脑上运行。它唯一的功能是读取在线电子表格(私人的,比如购物清单),然后对其进行一些无关的工作。
自从Google本月放弃了OAuth1.0以来,我一直在尝试使用OAuth2让该程序工作。之前可以使用我的电子邮件和应用程序密码进行身份验证。
现在,我被迫通过访问令牌进行工作。我的代码:
package joeslist;

import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.CellFeed;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeRequestUrl;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author 74
 */
public class JoesList {

    public static void main(String[] args) {

        final String CLIENT_ID = "my_client_id.apps.googleusercontent.com";  //Unused?
        final String CLIENT_SECRET = "myClientSecret";

    // This is the Redirect URI for installed applications.
        // If you are building a web application, you have to set your
        // Redirect URI at https://code.google.com/apis/console.
        final String REDIRECT_URI = "urn:ietf:wg:oauth:2.0:oob";

        final SpreadsheetService service;
        CellFeed feed;
        service = new SpreadsheetService("Joe's List");

        HttpTransport httpTransport = new NetHttpTransport();
        JacksonFactory jsonFactory = new JacksonFactory();
        String[] SCOPESArray = {"https://spreadsheets.google.com/feeds"};
        final List SCOPES = Arrays.asList(SCOPESArray);
        GoogleCredential credential;

        try {          
            // Step 1: Authorize.
            String authorizationUrl = new GoogleAuthorizationCodeRequestUrl(CLIENT_ID, REDIRECT_URI, SCOPES).build();

            // Point or redirect your user to the authorizationUrl.
            System.out.println("Go to the following link in your browser:");
            System.out.println(authorizationUrl);

            // Read the authorization code from the standard input stream.
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Paste the code that you got.");
            String code = in.readLine();
             // End of Step 1 <--

            // Step 2: Exchange!
            GoogleTokenResponse response
                    = new GoogleAuthorizationCodeTokenRequest(httpTransport, jsonFactory, CLIENT_ID, CLIENT_SECRET,
                            code, REDIRECT_URI).execute();
            System.out.println("Token expires in: " + response.getExpiresInSeconds() + " seconds!"); 

            // Let's build our GoogleCredential now.
            credential = new GoogleCredential.Builder()
                    .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
                    .setTransport(httpTransport)
                    .setJsonFactory(jsonFactory)
                    .build()
                    .setAccessToken(response.getAccessToken())
                    .setRefreshToken(response.getRefreshToken());       
            service.setOAuth2Credentials(credential);
        } catch (IOException ex) {
            Logger.getLogger(FuckingTest.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            final String spreadsheetName = "Joe's sheet";
            final URL metafeedUrl=new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full?xoauth_requestor_id=joe");
            final SpreadsheetFeed spreadsheetFeed = service.getFeed(metafeedUrl, SpreadsheetFeed.class);
            final List<SpreadsheetEntry> spreadsheets = spreadsheetFeed.getEntries();
            System.err.println(spreadsheets.size());
            for (final SpreadsheetEntry spreadsheet : spreadsheets) {
                System.err.println(spreadsheet.getTitle().getPlainText());
                if (spreadsheetName.equals(spreadsheet.getTitle().getPlainText())) {
                    System.err.println("Found the Spreadsheet you want.");
                }
            }
        } catch (final MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (final IOException | ServiceException e) {
            throw new RuntimeException(e);
        }

    }
}

问题:

这是一个私人小程序。我将成为它唯一的用户,我想让它读取一个私人电子表格。

每次运行它时,我是否需要费尽心思手动复制和粘贴访问令牌? 是否有办法获得长期或永久的访问令牌?

1个回答

1
答案是每当您的访问令牌过期时,您都需要经历此过程。
从安全角度来看,访问令牌有限的生命周期是正确的。永久访问令牌是一个安全漏洞,等待被发现(您知道有人只是忘记加密它,并最终将其存储在 /var/log 或更糟的桌面下)。
根据OP的评论编辑:
谷歌提供服务帐户,允许服务器之间进行通信。更多信息请参见 使用OAuth 2.0用于服务器到服务器应用程序

有没有办法使用服务帐户或类似的东西来访问我的私人电子表格,并只读取它?我的意思是,我自己正在发行密钥... - Dimitris Sfounis
已更新我的回答 - 请查看是否符合您的要求。 - Prahalad Deshpande

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接