标签: google-api-java-client

Android Calendar API上的Google Calendar API OAuth2令人烦恼

我正在开发Android Honeycomb(v3.0)应用程序,该应用程序需要与Google Calendar API进行通信.我想允许我的应用访问特定Google帐户的日历数据,以便阅读和创建活动.

不幸的是,我使用OAuth2遇到授权问题.这是我到目前为止所拥有的:

1)我想访问其日历的Google帐户是在我正在使用的Android设备中注册的.

2)我在帐户的Google API控制台中启用了Calendar API.

3)我可以使用以下代码访问此帐户:

AccountManager accountManager = AccountManager.get(this.getBaseContext());
Account[] accounts = accountManager.getAccountsByType("com.google");
Account acc = accounts[0]; // The device only has one account on it
Run Code Online (Sandbox Code Playgroud)

4)我现在想要获得一个AuthToken,以便在与日历通信时使用.我遵循了本教程,但将所有内容转换为适用于Google日历而非Google任务.我通过使用with 成功authTokenAccountManager我想要使​​用的帐户中检索一个.getAuthTokenAUTH_TOKEN_TYPE == "oauth2:https://www.googleapis.com/auth/calendar"

5)这是问题开始的地方.我现在在这一点上:

AccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(tokens[0]); // this is the correct token
HttpTransport transport = AndroidHttp.newCompatibleTransport();
Calendar service = Calendar.builder(transport, new JacksonFactory())
    .setApplicationName("My Application's Name")
    .setHttpRequestInitializer(accessProtectedResource)
    .build();
service.setKey("myCalendarSimpleAPIAccessKey"); // This is deprecated???
Events events = service.events().list("primary").execute(); // …
Run Code Online (Sandbox Code Playgroud)

android google-calendar-api google-api google-api-java-client android-3.0-honeycomb

7
推荐指数
1
解决办法
4822
查看次数

服务帐户上的Java Google Calendar api"access_denied"

我正在尝试使用Java Google Calendar api连接到日历.Java应用程序使用服务帐户.

我有以下代码:

   java.io.File licenseFile = new java.io.File("39790cb51b361f51cab6940d165c6cda4dc60177-privatekey.p12");

   GoogleCredential credential = new GoogleCredential.Builder()

  .setTransport(HTTP_TRANSPORT)
  .setJsonFactory(JSON_FACTORY)
  .setServiceAccountId("xxx@developer.gserviceaccount.com") 
  .setServiceAccountUser(EMAIL_ADRESS)
  .setServiceAccountScopes(CalendarScopes.CALENDAR)
  .setServiceAccountPrivateKeyFromP12File(licenseFile)
  .build();

  client = new com.google.api.services.calendar.Calendar.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, credential)
                        .setApplicationName("Google Calendar Sync").build();

  Calendar calendar = client.calendars().get(EMAIL_ADRESS).execute();
Run Code Online (Sandbox Code Playgroud)

在最后一行,我收到一条带有消息的IOException:

ex =(com.google.api.client.auth.oauth2.TokenResponseException)com.google.api.client.auth.oauth2.TokenResponseException:400 Bad Request {"error":"access_denied"}

我dubble检查了GoogleCredential对象的值,它们是正确的.我还添加了https://www.google.com/calendar/feeds/,http://www.google.com/calendar/feeds/与应用程序ID为我的客户域控制台授权第三方应用访问

我忘记了一步吗?

java google-calendar-api google-api google-apps google-api-java-client

7
推荐指数
1
解决办法
1904
查看次数

访问Google API时无效的授权

我正在尝试使用"服务帐户"授权访问来调用任何Google API.我在Google API控制台的"服务"标签中下载了".pk2"文件并激活了"URL Shortener API".每当我尝试调用任何API(URL缩短器或Adsense)时.我有以下例外 -

com.google.api.client.auth.oauth2.TokenResponseException: 400 Bad Request
{
  "error" : "invalid_grant"
}
    at com.google.api.client.auth.oauth2.TokenResponseException.from(TokenResponseException.java:105)
    at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:303)
    at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:323)
    at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:345)
    at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:526)
    at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:287)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:836)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:412)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:345)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:463)
Run Code Online (Sandbox Code Playgroud)

以下是代码片段 -

    HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
    JsonFactory JSON_FACTORY = new JacksonFactory();
    File privateKey = new File(ReportAdsense.class.getResource("mykey.p12").toURI());
    GoogleCredential credential = new GoogleCredential.Builder().setTransport(HTTP_TRANSPORT)
            .setJsonFactory(JSON_FACTORY)
            .setServiceAccountId("my_valid_account_id@developer.gserviceaccount.com")
            .setServiceAccountScopes(UrlshortenerScopes.URLSHORTENER)
            .setServiceAccountPrivateKeyFromP12File(privateKey)
            .build();
     Urlshortener service = new Urlshortener.Builder(new NetHttpTransport(), JSON_FACTORY, null).setHttpRequestInitializer(credential).build();
     UrlHistory history = service.url().list().execute();
Run Code Online (Sandbox Code Playgroud)

google-api google-api-java-client google-oauth

7
推荐指数
1
解决办法
1万
查看次数

Spring:Google身份验证redirect_uri_mismatch和URL无法在浏览器上打开

我正在研究在tomcat上运行的Spring-MVC应用程序,我想在其中使用Google驱动器功能.我尝试在我的本地机器上使用服务帐户,我没有遇到任何问题.但是当我在服务器上上传代码时,浏览器URL不会被打开.然后我想,我不应该使用服务帐户,我应该使用普通的网络应用程序帐户.现在,当我这样做时,我得到一个redirect_uri_mismatch.

我不明白一件事,我在流程中设置重定向URL,在JSON中,为什么它会获得带有随机端口号的redirect_url.如果我更改浏览器URL中的端口号,它可以正常工作.但仍然在服务器上它不会打开浏览器URL,我可以在tomcat日志中看到它,但该死的东西不会打开URL.

以下是我在Google应用中的重定向网址:

http://localhost/authorizeuser
http://localhost:8080/
http://localhost:8080
http://localhost
http://localhost:8080/Callback
https://testserver.net/Callback
http://testserver.net/Callback
http://127.0.0.1
Run Code Online (Sandbox Code Playgroud)

这是我的client_secret.json:

{"web": {
    "client_id": "clientid",
    "auth_uri": "https://accounts.google.com/o/oauth2/auth",
    "token_uri": "https://accounts.google.com/o/oauth2/token",
    "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
    "client_email": "clientemailstuff",
    "client_x509_cert_url": "certurlstuff",
    "client_secret": "itsasecret",
    "redirect_uris": ["http://localhost:8080/","http://localhost:8080"],
    "javascript_origins": ["https://testserver.net", "http://testserver.net","http://localhost:8080"]
}}
Run Code Online (Sandbox Code Playgroud)

以下是我要验证的代码:

 @Override
    public Credential authorize() throws IOException {
        InputStream in =
                DriveQuickstartImpl.class.getResourceAsStream("/client_secret.json");
        GoogleClientSecrets clientSecrets =
                GoogleClientSecrets.load(JSON_FACTORY, new InputStreamReader(in));

        GoogleAuthorizationCodeFlow flow =
                new GoogleAuthorizationCodeFlow.Builder(
                        HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, SCOPES)
                        .setDataStoreFactory(DATA_STORE_FACTORY)
                        .setAccessType("offline")
                        .build();
        flow.newAuthorizationUrl().setState("xyz").setRedirectUri("http://localhost:8080/Callback");
        Credential credential = new AuthorizationCodeInstalledApp(
                flow, new LocalServerReceiver()).authorize("user");

        if(credential!=null && credential.getRefreshToken() != null){
            storeCredentials(credential);
        } …
Run Code Online (Sandbox Code Playgroud)

java google-api google-apps google-api-java-client google-oauth

7
推荐指数
1
解决办法
3375
查看次数

使用 Google 的 Java API 客户端库进行身份验证时出现 IllegalArgumentException

我正在尝试在 NetBeans 8.2 上运行 QuickStart.java https://developers.google.com/sheets/api/quickstart/java。我已经导入了所有的 Google 图书馆,但遇到了这个问题

Exception in thread "main" java.lang.IllegalArgumentException
at com.google.api.client.repackaged.com.google.common.base.Preconditions.checkArgument(Preconditions.java:111)
at com.google.api.client.util.Preconditions.checkArgument(Preconditions.java:37)
at com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets.getDetails(GoogleClientSecrets.java:82)
at com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder.<init>(GoogleAuthorizationCodeFlow.java:197)
at Quickstart.authorize(Quickstart.java:71)
at Quickstart.getSheetsService(Quickstart.java:90)
at Quickstart.main(Quickstart.java:98)
C:\Users\kevin\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
Run Code Online (Sandbox Code Playgroud)

我已经尝试过警告中提到的内容:无法更改每个人的权限:,但没有用。我的代码如下:

import com.google.api.client.auth.oauth2.Credential;
import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;
import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.util.store.FileDataStoreFactory;
import com.google.api.services.sheets.v4.SheetsScopes;
import com.google.api.services.sheets.v4.model.*;
import com.google.api.services.sheets.v4.Sheets;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.List;

public class Quickstart {
    /** …
Run Code Online (Sandbox Code Playgroud)

java google-api-java-client

7
推荐指数
1
解决办法
3452
查看次数

我可以从非 UI 应用程序获得 Youtube 报告 API 的授权吗?

我想定期运行一个应用程序(在 AWS Lambda 上),它使用 Youtube 的报告 API获取报告并将报告存储在数据库中。我是 Youtube 频道的所有者。

但是,我在设置授权时遇到了问题。文档提到了OAuth的许多流程。其中,服务帐户流程似乎很适合这里,因为我没有尝试访问任何其他用户的数据,而且我的应用程序没有 UI。但是,他们提到“YouTube Reporting API 和 YouTube Analytics API 不支持此流程”。对于其他流程,即服务器端和客户端,看起来我需要一个 UI 应用程序。

我在本地运行了他们的Java 代码示例,它打开了浏览器进行授权。

我想知道是否可以在没有 UI/浏览器支持的情况下完成授权。这似乎是一个应该支持的常见用例。

这个相关的堆栈溢出答案提到“相反,创建和使用网络客户端谷歌凭据。存储和使用从流程中生成的令牌。”。如果我理解正确,我需要从谷歌开发控制台生成客户端 ID 和客户端密码,然后可能使用oauth 游乐场生成访问令牌。但是,此令牌将在 24 小时内过期。我可以不断刷新令牌,但这似乎是一种解决方法。

如果不支持,我正在考虑的另一种方法是我的应用程序发送一封带有身份验证链接的电子邮件,并在电子邮件收件人打开链接并登录后继续运行。我想知道获取该链接是否可行谷歌的 oauth 库。

注意:我没有使用 Google 的 App Engine 或 Compute Engine。

java google-api-java-client google-oauth service-accounts youtube-analytics-api

7
推荐指数
1
解决办法
293
查看次数

从 Google Drive API 获取操作项列表


嗨,大家好。

我一直在尝试使用 Google Drive API 来获取一个列表,其中包含使用 Spring Boot 和我公司域中所有文件(文档或电子表格)中分配的操作项google-api-services-drive,但我遇到了一些问题:

  • 看起来 API 上没有关于操作项的内容。
  • 评论是我能得到的最接近的,但它们不包括操作项信息。他们只有被提及的人的电子邮件。
  • 文档看起来广泛而不精确。例如,这里他们说文件资源包含一个indexableText属性,但它不存在于响应中。
  • Term for followup 中所述,查找 actionitems您可以应用查询来获取包含 action items 的文件。为什么该fullText字段在响应中不可用,或者某些其他等效属性无法查看实际内容并将其用作获取操作项的解决方法?

我只需要从评论中知道谁被分配到操作项。

有任何想法吗?

google-api google-api-java-client google-drive-api google-drive-shared-drive

7
推荐指数
1
解决办法
436
查看次数

使用服务帐号从 Google 域中获取用户列表


大家好。

我被分配了使用 Spring Boot 从 Google 域的每个成员的收件箱中获取未答复电子邮件的任务,但我无法做到。

首先,我需要域中的用户列表。这可以通过 Directory API 实现(顺便说一下,它不能在 Google Developer Console 中通过该名称启用。看起来它属于 Admin SDK 左右)。

到目前为止我所面临的是:

  • SO上有很多相关的问题,但大多数已经过时了。
  • Java Quickstart for Google Directory API 不包含使用服务帐户的示例,我想使用它们,因为我的应用程序在 docker 容器中运行,而使用 Oauth 意味着我每次部署新版本或重新启动时都需要手动授权它容器。
  • Google 文档参考了管理控制台中的“API 参考”设置,但我在那里没有看到该部分。

我没有将凭据存储在 JSON 文件中,而是将它们存储在环境变量中。我正在这样做:

var inputStream = IOUtils.toInputStream(apiCredentials, Charset.defaultCharset());  //apiCredentials is a string with the JSON contents.
var credential = GoogleCredential
                .fromStream(inputStream, httpTransport, JacksonFactory.getDefaultInstance())
                .createScoped(Collections.singleton(DirectoryScopes.ADMIN_DIRECTORY_USER));
var directoryService = new Directory.Builder(httpTransport, JacksonFactory.getDefaultInstance(), credential)
                .setApplicationName("My App")
                .build();
var result = directoryService.users().list()
                    .setPageToken(pageToken)
                    .setDomain("my.domain")
                    .setMaxResults(10)
                    .execute();
Run Code Online (Sandbox Code Playgroud)

在此之后,我收到一个400 Bad request …

google-api google-api-java-client service-accounts spring-boot

7
推荐指数
0
解决办法
65
查看次数

google-api-client 和 google-api-services 之间的版本兼容性

有一个项目依赖于google-api-clientgoogle-api-services-drive

google-api-client版本看起来像这样:1.23.0,并且 github 上有这个库的变更日志。

google-api-services-drive版本看起来像这样:v3-rev105-1.23.0,并且没有明确的变更日志。rev同一版本号有许多不同的后缀,例如v3-rev116-1.23.0v3-rev135-1.23.0等。看起来它是每周构建的,并且修订号不断增加。

问题是:客户端和服务版本如何兼容?是否可以安全地假设,任何具有版本的服务都v3-revXXX-1.23.0与 client 兼容1.23.0,并且任何具有版本的 api-servicev3-revYYYYMMDD-1.32.1都与 api-client 兼容1.32.1

java versioning google-api-java-client

7
推荐指数
1
解决办法
5188
查看次数

java google custom search api

我正在尝试将Java客户端用于Google自定义搜索API, 但无法在网络上找到任何示例教程.有人能为我提供一个简单的例子吗?谢谢!

java search google-api-java-client

6
推荐指数
1
解决办法
8189
查看次数