标签: google-api-java-client

Google推送通知 - 未经授权的WebHook回调频道

我遇到谷歌推送通知(驱动器)的问题.我使用的服务帐户非常适用于所有其他驱动器操作,除了驱动器更改监视.

以下是现在因"未授权的WebHook回调通道"异常而失败的应用程序代码.我还转储了调用drive.changes.watch.execute时生成的请求和响应.

目标通知地址在API和auth推送控制面板中列入白名单(我甚至​​将其列在Javascript起源和引用中),现在我遇到了这个401 Unauthorized错误.

有人知道我在哪里弄错了吗?谢谢你的帮助.

PrivateKey serviceAccountPrivateKey = SecurityUtils.loadPrivateKeyFromKeyStore(SecurityUtils.getPkcs12KeyStore(), p12File, "notasecret", "privatekey", "notasecret");
JsonFactory jsonFactory = new JacksonFactory();
HttpTransport t = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential gc = new GoogleCredential.Builder()
                .setTransport(t)
                .setJsonFactory(jsonFactory)
                .setServiceAccountScopes(Collections.singleton(DriveScopes.DRIVE))
                .setServiceAccountPrivateKey(serviceAccountPrivateKey)
                .setServiceAccountId(Config.SERVICE_ACCOUNT_ID)
                .setServiceAccountUser(Config.SERVICE_ACCOUNT_USER)
                .build();

drive = new Drive.Builder(t, jsonFactory, null).setHttpRequestInitializer(gc).setApplicationName(cfg.getStringParam(Config.GAE_APPLICATION_NAME)).build();

// THIS WORKS
Changes.List request = drive.changes().list();
ChangeList changes = request.execute();

// THIS DOES NOT WORK
Channel channel = new Channel();
channel.setId(UUID.randomUUID().toString());
channel.setType("web_hook");
channel.setAddress(Config.PUSH_NOTIFICATION_ADDRESS);
Channel c = drive.changes().watch(channel).execute();


-------------- REQUEST  --------------
POST https://www.googleapis.com/drive/v2/changes/watch
Accept-Encoding: gzip
Authorization: Bearer XXX
User-Agent: XXX …
Run Code Online (Sandbox Code Playgroud)

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

13
推荐指数
2
解决办法
9009
查看次数

java.net.UnknownHostException无法解析主机"accounts.google.com":在bigquery中插入行时没有与主机名关联的地址

嗨,我正在研究我已集成BigQuery的Android应用程序.我看到有时在BigQuery表中插入记录时会遇到很多异常.我们不是这方面的专长,而是开始学习这项新技术.如果你们可以帮助我,那将是很棒的.

   java.net.UnknownHostException: Unable to resolve host "accounts.google.com": No address associated with hostname
          at java.net.InetAddress.lookupHostByName(InetAddress.java:424)
          at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
          at java.net.InetAddress.getAllByName(InetAddress.java:214)
          at com.android.okhttp.internal.Dns$1.getAllByName(Dns.java:28)
          at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:216)
          at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:122)
          at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:292)
          at com.android.okhttp.internal.http.HttpEngine.sendSocketRequest(HttpEngine.java:255)
          at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:206)
          at com.android.okhttp.internal.http.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:345)
          at com.android.okhttp.internal.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:89)
          at com.android.okhttp.internal.http.HttpURLConnectionImpl.getOutputStream(HttpURLConnectionImpl.java:197)
          at com.android.okhttp.internal.http.HttpsURLConnectionImpl.getOutputStream(HttpsURLConnectionImpl.java:254)
          at com.google.a.a.c.a.c.a(NetHttpRequest.java:77)
          at com.google.a.a.c.r.p(HttpRequest.java:972)
          at com.google.a.a.a.a.h.a(TokenRequest.java:307)
          at com.google.a.a.b.a.a.b.f(GoogleCredential.java:384)
          at com.google.a.a.a.a.c.h(Credential.java:489)
          at com.google.a.a.a.a.c.a(Credential.java:217)
          at com.google.a.a.c.r.p(HttpRequest.java:859)
          at com.google.a.a.b.d.c.c(AbstractGoogleClientRequest.java:469)
          at com.test.utils.c.c(CommonUtility.java:2730)
          at com.test.services.AppInstallIntentService.onHandleIntent(AppInstallIntentService.java:71)
          at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
          at android.os.Handler.dispatchMessage(Handler.java:102)
          at android.os.Looper.loop(Looper.java:136)
          at android.os.HandlerThread.run(HandlerThread.java:61)
   Caused by: libcore.io.GaiException: getaddrinfo failed: EAI_NODATA (No address associated with hostname)
          at libcore.io.Posix.getaddrinfo(Posix.java)
          at …
Run Code Online (Sandbox Code Playgroud)

java android google-api-java-client google-api-client google-bigquery

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

使用Firebase在GoogleApiClient中出现罕见的NullPointerException

我使用GoogleApiClient使用他们的Google帐户登录用户.基本上,我使用Firebase Auth与Google登录.

但我每天都会在某些设备上崩溃.当我在我自己的一些设备(OnePlus 3,Nexus 5X,Moto G等)上进行测试时,我从未看到过这种崩溃.

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase(java.util.Locale)' on a null object reference
       at com.google.android.gms.internal.zzamy.zzd(Unknown Source)
       at com.google.android.gms.internal.zzamv.n(Unknown Source)
       at com.google.android.gms.internal.zzamv.zza(Unknown Source)
       at com.google.android.gms.internal.zzamv$2.run(Unknown Source)
       at java.lang.Thread.run(Thread.java:818)
Run Code Online (Sandbox Code Playgroud)

这就是我正在做的实例化GoogleApiClient.我没有看到正常情况下的崩溃,但在某些设备中我看到了这一点.这是我在代码中所做的事情,

String mClientId = parcel.getProviderExtra().getString(CLIENT_ID_KEY);
GoogleSignInOptions googleSignInOptions;

googleSignInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
                .requestIdToken(mClientId)
                .requestEmail()
                .build();

mGoogleApiClient = new GoogleApiClient.Builder(App.getContext())
                .addApi(Auth.GOOGLE_SIGN_IN_API, googleSignInOptions)
                .build();

mGoogleApiClient.connect();
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?我最好的选择是,App.getContext()它为应用程序上下文提供实例化客户端.这可能是问题吗?

我可以使用活动上下文,但使用它会导致内存泄漏.这里有什么问题,如何解决?

对于在打开应用程序并尝试唱歌之后发生崩溃的用户,这会产生非常糟糕的体验.

android google-api-java-client google-api-client firebase firebase-authentication

13
推荐指数
1
解决办法
931
查看次数

使用Android和Google API客户端的Proguard

首先,对不起,如果这个话题得到了回答,但我还没找到......

我是Android和Proguard的新手.在开发我的应用程序的最后,我想用proguard减小它的大小,当然.由于我在我的应用程序中使用谷歌api客户端的一些库,我已将它们添加为外部jar.

好吧,我的应用程序工作正常,没有通过proguard步骤,但当我尝试导出它,它开始我的头痛.很多警告,我不知道如何跳过或安排.像这样的东西:

[2012-03-05 07:20:06 - CollaJoveThreads] Proguard returned with error code 1. See console
[2012-03-05 07:20:06 - CollaJoveThreads] Warning: com.google.common.base.Equivalence: can't find referenced class javax.annotation.Nullable
[2012-03-05 07:20:06 - CollaJoveThreads] Warning: com.google.common.base.Equivalence: can't find referenced class javax.annotation.Nullable
[2012-03-05 07:20:06 - CollaJoveThreads] Warning: com.google.common.base.Equivalence: can't find referenced class javax.annotation.Nullable
[2012-03-05 07:20:06 - CollaJoveThreads] Warning: com.google.common.base.Equivalences$Impl$1: can't find referenced class javax.annotation.Nullable
[2012-03-05 07:20:06 - CollaJoveThreads] Warning: com.google.common.base.Equivalences$Impl$1: can't find referenced class javax.annotation.Nullable
Run Code Online (Sandbox Code Playgroud)

而且更相似.

我跟踪了Google开发人员的推荐,并将这些行添加到我的proguard.cfg中:

# Needed by google-api-client to keep generic types …
Run Code Online (Sandbox Code Playgroud)

android proguard google-api-java-client

11
推荐指数
1
解决办法
5940
查看次数

Google凭据:此开发者帐户不拥有该应用程序

我正在使用Google客户端库并尝试GET向Google Play API发出请求.

        GoogleCredential credential= new GoogleCredential.Builder().setTransport(netHttpTransport)
                .setJsonFactory(jacksonFactory)
                .setServiceAccountId(CLIENT_ID)
                .setServiceAccountScopes(SCOPE)
                .setServiceAccountPrivateKeyFromP12File(file)
                .build();
                credential.refreshToken();     
        HttpRequestFactory requestFactory =netHttpTransport.createRequestFactory(credential);
        GenericUrl url = new GenericUrl(URI);
        HttpRequest request = requestFactory.buildGetRequest(url);
        HttpResponse response = request.execute();
Run Code Online (Sandbox Code Playgroud)

我明白了

 {
  "code" : 401,
  "errors" : [ {
    "domain" : "androidpublisher",
    "message" : "This developer account does not own the application.",
    "reason" : "developerDoesNotOwnApplication"
  } ],
  "message" : "This developer account does not own the application."
}
Run Code Online (Sandbox Code Playgroud)

我的应用程序未发布,会导致问题吗?

google-api google-api-java-client

11
推荐指数
2
解决办法
3115
查看次数

从谷歌api存储的凭据重用java

我已成功授权我的桌面应用程序.它存储一个名为的文件StoredCredential.为了不必每次运行应用程序时都将复制URL保存到浏览器,接受等步骤,我想使用我已存储的凭据.

我的代码到目前为止:

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(
    httpTransport,
    JSON_FACTORY,
    CLIENT_ID,
    CLIENT_SECRET,
    SCOPE)
    .setDataStoreFactory(dataStoreFactory)
    .setApprovalPrompt("auto").setAccessType("offline").build();
//
System.out.println("flow success");     

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 tokenResponse
    = flow.newTokenRequest(code).setRedirectUri(REDIRECT_URI).execute();

GoogleCredential credential = new GoogleCredential
    .Builder()
    .setTransport(new NetHttpTransport())
    .setJsonFactory(new JacksonFactory())
    .setClientSecrets(CLIENT_ID, CLIENT_SECRET)
    .addRefreshListener(new CredentialRefreshListener() {
            @Override
            public void onTokenResponse(
                Credential credential, …
Run Code Online (Sandbox Code Playgroud)

java google-api google-api-java-client

11
推荐指数
1
解决办法
6278
查看次数

访问未配置的Android Google API密钥

我正在尝试使用Youtube Data API v3在Android上进行搜索,但是当我尝试使用我的API密钥进行搜索时,我始终会收到以下消息:

{
 "error": {
  "errors": [
   {
    "domain": "usageLimits",
    "reason": "accessNotConfigured",
    "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project."
   }
  ],
  "code": 403,
  "message": "Access Not Configured. Please use Google Developers Console to activate the API for your project."
 }
}
Run Code Online (Sandbox Code Playgroud)

我在控制台中将Youtube Data API设置为on.我一直试图弄清楚这几个小时.有谁知道什么是错的?

android youtube-api google-api-java-client android-youtube-api youtube-data-api

11
推荐指数
2
解决办法
4056
查看次数

使用Java从Google Storage下载文件

我已成功自动完成将Google Big Query中的数据移至Google存储空间的流程.现在,我还需要以自动方式将数据从Google Storage下载到我的环境中.

我正在尝试执行正常的HTTP请求,但之前已授权.所以我的HTTP请求是

    HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(authorize());
    GenericUrl url = new GenericUrl(uri);
    HttpRequest request = requestFactory.buildGetRequest(url);
    HttpResponse response = request.execute();
    String content = response.parseAsString();
Run Code Online (Sandbox Code Playgroud)

我的授权码是

/** Authorizes the installed application to access user's protected data. */
    private static Credential authorize() throws Exception
    {
        HttpTransport httpTransport = new NetHttpTransport();
        JsonFactory jsonFactory = new JacksonFactory();

        // load client secrets
        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
                new InputStreamReader(BigQueryConsumer.class.getResourceAsStream("/secret.json")));

        // This creates the credentials datastore at ~/.oauth-credentials/${credentialDatastore}
        FileDataStoreFactory fileDataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home") + …
Run Code Online (Sandbox Code Playgroud)

java google-api google-authentication google-api-java-client google-cloud-storage

11
推荐指数
1
解决办法
1086
查看次数

可以取消Google云端硬盘上传?

我已经使用官方API编写了一个应用程序来上传到Google驱动器,这非常有效.但是我无论如何都无法取消上传.我在ASyncTask中运行它,到目前为止,我已尝试制作驱动器对象,文件路径,令牌和其他一些变量.我也尝试使令牌无效,但似乎这只是从缓存中删除它并没有实际上使服务器端无效.我已经在ASyncTask上调用了cancel,但是如果在上传开始时它被取消,那么似乎无法阻止它.

我的问题是,如果用户开始上传100mb文件,他们无法取消它,除了打开和关闭互联网连接这是不切实际的.这是我的上传代码:

  java.io.File jFile = new java.io.File(path); //Does not get file just info
                File body = setBody(jFile, mimeType);
                try
                {
                  java.io.File mediaFile = new java.io.File(path);
                  InputStreamContent mediaContent =
                      new InputStreamContent(mimeType,
                          new BufferedInputStream(new FileInputStream(mediaFile)));
                  mediaContent.setLength(mediaFile.length());
                  request = drive.files().insert(body, mediaContent);
                  request.getMediaHttpUploader().setProgressListener(new CustomProgressListener());
                  File file = request.execute();
Run Code Online (Sandbox Code Playgroud)

肯定有办法取消上传?

java android google-api-java-client android-asynctask google-drive-api

10
推荐指数
1
解决办法
4318
查看次数

使用Google API中的com.google.api.client.http.HttpRequest对象发送POST请求

我必须发送具有以下结构的POST请求.

    POST https://www.googleapis.com/fusiontables/v1/tables
    Authorization: /* auth token here */
    Content-Type: application/json

    {
     "name": "Insects",
     "columns": [
     {
        "name": "Species",
        "type": "STRING"
     },
     {
         "name": "Elevation",
         "type": "NUMBER"
     },
    {
         "name": "Year",
         "type": "DATETIME"
    }
      ],
   "description": "Insect Tracking Information.",
   "isExportable": true
    }
Run Code Online (Sandbox Code Playgroud)

我使用下面的代码发送POST请求,但我得到一个响应为"400 Bad Request"

String PostUrl = "https://www.googleapis.com/fusiontables/v1/tables";
HttpRequestFactory requestFactory = HTTP_TRANSPORT.createRequestFactory(credential);

//generate the REST based URL
GenericUrl url = new GenericUrl(PostUrl.replaceAll(" ", "%20"));
//make POST request

String requestBody = "{'name': 'newIndia','columns': [{'name': 'Species','type': 'STRING'}],'description': 'Insect Tracking Information.','isExportable': true}"; …
Run Code Online (Sandbox Code Playgroud)

java post google-api-java-client google-fusion-tables google-oauth

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