Android でGoogleAPIを使うときの注意点


混乱のもとは Google Play Service 経由で使えるものと使えないものがあること

日々、アップデートが続く Google Play Service 、でも Googleが公開しているAPIすべてを使えるわけじゃない

けつろん

  • ややこしい
  • google-api-client-android が橋渡しする

google-api-java-client の存在

JavaでGoogleAPIを使用するための google-api-java-client というライブラリがある。Google Play Service ではできなくて、google-api-java-client ではできるものもある。

OAuth 認証面倒だよね?Android は Google アカウント連携してるから、誰かその辺うまくやってくれない?

google-api-client-android の存在

その辺の橋渡しをするのが google-api-client-android 。以下のような実装があり、Android の Context を渡すことで、OAuth2 認証をうまくつなぐのりの役割を果たす。

GoogleAccountCredential.java
  public static GoogleAccountCredential usingOAuth2(Context context, Collection<String> scopes) {
    Preconditions.checkArgument(scopes != null && scopes.iterator().hasNext());
    String scopesStr = "oauth2: " + Joiner.on(' ').join(scopes);
    return new GoogleAccountCredential(context, scopesStr);
  }

さんこう

Maven

     <dependency>
      <groupId>com.google.api-client</groupId>
      <artifactId>google-api-client</artifactId>
      <version>1.18.0-rc</version>
      <exclusions>
        <exclusion>
          <artifactId>xpp3</artifactId>
          <groupId>xpp3</groupId>
        </exclusion>
        <exclusion>
          <artifactId>httpclient</artifactId>
          <groupId>org.apache.httpcomponents</groupId>
        </exclusion>
        <exclusion>
          <artifactId>junit</artifactId>
          <groupId>junit</groupId>
        </exclusion>
        <exclusion>
          <artifactId>android</artifactId>
          <groupId>com.google.android</groupId>
        </exclusion>
      </exclusions>
    </dependency>