gpt4 book ai didi

java - Google 电子表格 API - 获取永久访问 token

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:21 25 4
gpt4 key购买 nike

我有一个在我的计算机上运行的简单 Java 程序。它的唯一功能是阅读在线电子表格(私有(private) - 例如,购物 list ),然后做一些不相关的工作。

自从谷歌本月放弃 OAuth1.0 以来,我一直在努力让该程序与 OAuth2 一起工作。以前,我可以让程序使用我的电子邮件和应用程序密码进行身份验证。

现在,我被迫通过访问 token 来工作。我的代码:

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);
}

}
}

问题:

这是一个私有(private)小程序。我将成为它的唯一用户,我想让它做的就是阅读私有(private)电子表格。

每次运行它时,我是否都必须绕圈子,手动复制和粘贴访问 token ?有什么方法可以获得持久或永久的访问 token ?

最佳答案

答案是每次访问 token 过期时您都必须执行此过程。

访问 token 的生命周期有限,从安全角度来看这是正确的。永久访问 token 是一个等待被发现的安全漏洞(您知道有人只是忘记加密它并最终将其存储在/var/log 或更糟的桌面下)。

根据 OP 的评论进行编辑

Google 提供允许服务器到服务器通信的服务帐户。更多信息请访问 Using OAuth 2.0 for Server to Server Applications

关于java - Google 电子表格 API - 获取永久访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30513101/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com