我创建了一些填充谷歌电子表格的工具。 它工作正常1年,从今天起我就错了
Exception in thread "main" com.google.gdata.util.AuthenticationException: Error authenticating (check service name)
at com.google.gdata.client.GoogleAuthTokenFactory.getAuthException(GoogleAuthTokenFactory.java:688)
at com.google.gdata.client.GoogleAuthTokenFactory.getAuthToken(GoogleAuthTokenFactory.java:560)
at com.google.gdata.client.GoogleAuthTokenFactory.setUserCredentials(GoogleAuthTokenFactory.java:397)
at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:364)
at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:319)
at com.google.gdata.client.GoogleService.setUserCredentials(GoogleService.java:303)
这是与gmail连接的代码部分:
String USERNAME = "usename@........com"; ->of course I'm using proper username and password
String PASSWORD = "*******";
SpreadsheetService service
= new SpreadsheetService("SandboxCheck");
service.setUserCredentials(USERNAME, PASSWORD);
我不知道如何连接gmail,我试图通过oAuth这样做,但我不知道如何做到这一点。在https://developers.google.com/google-apps/spreadsheets/authorize的示例中,只有.net。
的代码答案 0 :(得分:18)
我终于帮助from here设法创建了这样的连接。 一切都和以前一样。 您需要做的步骤是:
以下是工作代码
Path
我希望这对我所面临的问题有所帮助。 这是我使用的jar列表:
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.client.json.jackson.JacksonFactory;
import com.google.gdata.client.spreadsheet.SpreadsheetService;
import com.google.gdata.data.spreadsheet.SpreadsheetEntry;
import com.google.gdata.data.spreadsheet.SpreadsheetFeed;
import com.google.gdata.util.ServiceException;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;
public class OAuthIntegration{
public static void main(String[] args) throws MalformedURLException, GeneralSecurityException, IOException, ServiceException {
URL SPREADSHEET_FEED_URL;
SPREADSHEET_FEED_URL = new URL("https://spreadsheets.google.com/feeds/spreadsheets/private/full");
File p12 = new File("./key.p12");
HttpTransport httpTransport = new NetHttpTransport();
JacksonFactory jsonFactory = new JacksonFactory();
String[] SCOPESArray = {"https://spreadsheets.google.com/feeds", "https://spreadsheets.google.com/feeds/spreadsheets/private/full", "https://docs.google.com/feeds"};
final List SCOPES = Arrays.asList(SCOPESArray);
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(httpTransport)
.setJsonFactory(jsonFactory)
.setServiceAccountId("cliend_ID")
.setServiceAccountScopes(SCOPES)
.setServiceAccountPrivateKeyFromP12File(p12)
.build();
SpreadsheetService service = new SpreadsheetService("Test");
service.setOAuth2Credentials(credential);
SpreadsheetFeed feed = service.getFeed(SPREADSHEET_FEED_URL, SpreadsheetFeed.class);
List<SpreadsheetEntry> spreadsheets = feed.getEntries();
if (spreadsheets.size() == 0) {
System.out.println("No spreadsheets found.");
}
SpreadsheetEntry spreadsheet = null;
for (int i = 0; i < spreadsheets.size(); i++) {
if (spreadsheets.get(i).getTitle().getPlainText().startsWith("ListOfSandboxes")) {
spreadsheet = spreadsheets.get(i);
System.out.println("Name of editing spreadsheet: " + spreadsheets.get(i).getTitle().getPlainText());
System.out.println("ID of SpreadSheet: " + i);
}
}
}
}
答案 1 :(得分:4)
Google刚停止支持OAuth1.0。需要使用OAuth2。要进行切换,请先转到Google Developer Console,创建项目并设置凭据。然后更新您的代码,类似于以下Java代码:
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>1.19.0</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>com.google.http-client</groupId>
<artifactId>google-http-client-jackson</artifactId>
<version>1.19.0</version>
<type>jar</type>
</dependency>
}
您需要两个库:google-api-client和google-http-client-jackson。如果您使用Maven,请在pom.xml中包含以下依赖项。
{{1}}
答案 2 :(得分:1)
我尝试过来自高智晟的解决方案,但我对图书馆有一个问题。我使用netbeans,项目正在构建而没有错误,但是当我尝试执行它时,我发现了这个错误:
Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/jackson/JsonFactory
at com.google.api.client.json.jackson.JacksonFactory.<init>(JacksonFactory.java:38)
at SandboxCheck.main(SandboxCheck.java:48)
我发现classpath存在问题,但我不知道如何在netbeans上修复它,我认为在添加库时它会自动执行。 我添加了
google-oauth-client-1.16.0-rc.jar google-oauth-client-1.16.0-rc-sources.jar google-api-client-1.8.0-beta-sources.jar google-api-client-1.4.1-beta.jar google-api-client-1.19.1.jar
我不确定如果我添加了太多的这些库但是如果我删除了例如。 google-api-client-1.4.1-beta.jar它没有看到JacksonFactory类。