使用Java进行Google Glass GDK身份验证

时间:2014-12-26 01:20:36

标签: authentication google-glass google-gdk

我正在测试GDK Glassware的身份验证: 这是我的代码:

=============================================== ====================================

import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.List;

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.jackson2.JacksonFactory;
import com.google.api.client.util.Lists;
import com.google.api.services.mirror.Mirror;
import com.google.api.services.mirror.model.Account;
import com.google.api.services.mirror.model.AuthToken;
public class InsertAccountWithJava {
    /** Email of the Service Account */
    private static final String SERVICE_ACCOUNT_EMAIL =
        "540223414844-tj91ijj3u9**********@developer.gserviceaccount.com ";

    /** Path to the Service Account's Private Key file */
    private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH =
        "C:/Users/Yuan/Desktop/eyenotes-847a787fecec.p12";

    /** The account type, usually based on your company or app's package. */
    private static final String ACCOUNT_TYPE = "com.myapplication";

    /** The Mirror API scopes needed to access the API. */
    private static final List<String> MIRROR_ACCOUNT_SCOPES =Arrays.asList(
            "https://www.googleapis.com/auth/glass.thirdpartyauth");

    public static void main(String[] args) {
        try {
            Mirror mirror = getMirrorService();
            createAccount(mirror, "6164da1******", "zhongmeiCM", "com.myapplication", "747dab3e60dd8cd45595767580040538c8a29fcf");
        } catch (GeneralSecurityException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
    /**
     * Build and returns a Mirror service object authorized with the service accounts.
     *
     * @return Mirror service object that is ready to make requests.
     */
    public static Mirror getMirrorService() throws GeneralSecurityException, IOException, URISyntaxException {
      HttpTransport httpTransport = new NetHttpTransport();
      JacksonFactory jsonFactory = new JacksonFactory();
      GoogleCredential credential = new GoogleCredential.Builder()
          .setTransport(httpTransport)
          .setJsonFactory(jsonFactory)
          .setServiceAccountId(SERVICE_ACCOUNT_EMAIL)
          .setServiceAccountScopes(MIRROR_ACCOUNT_SCOPES)
          .setServiceAccountPrivateKeyFromP12File(
              new java.io.File(SERVICE_ACCOUNT_PKCS12_FILE_PATH))
          .build();
      Mirror service = new Mirror.Builder(httpTransport, jsonFactory, null)
          .setHttpRequestInitializer(credential).build();
      return service;
    }

    /**
     * Creates an account and causes it to be synched up with the user's Glass.
     * This example only supports one auth token; modify it if you need to add
     * more than one, or to add features or user data or the password field.
     *
     * @param mirror the service returned by getMirrorService()
     * @param userToken the user token sent to your auth callback URL
     * @param accountName the account name for this particular user
     * @param authTokenType the type of the auth token (chosen by you)
     * @param authToken the auth token
     */
    public static void createAccount(Mirror mirror, String userToken, String accountName,
        String authTokenType, String authToken) {
      try {
        Account account = new Account();
        List<AuthToken> authTokens = Lists.newArrayList();
        AuthToken authToken1 = new AuthToken().setType(authTokenType).setAuthToken(authToken);
        authTokens.add(authToken1);
        account.setAuthTokens(authTokens);
        account.setPassword("123456");
        mirror.accounts().insert(
            userToken, ACCOUNT_TYPE, accountName, account).execute();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
}


=====================================================================================

但我得到一个例外:

   com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
  "code" : 400,
  "errors" : [ {
    "domain" : "global",
    "message" : "Invalid Value",
    "reason" : "invalid"
  }, {
    "domain" : "global",
    "message" : "Invalid Value",
    "reason" : "invalid"
  } ],
  "message" : "Invalid Value"
}
    at com.google.api.client.googleapis.json.GoogleJsonResponseException.from(GoogleJsonResponseException.java:145)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:113)
    at com.google.api.client.googleapis.services.json.AbstractGoogleJsonClientRequest.newExceptionOnError(AbstractGoogleJsonClientRequest.java:40)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest$1.interceptResponse(AbstractGoogleClientRequest.java:312)
    at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:1049)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:410)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
    at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
    at TestInsert.createAccount(TestInsert.java:92)
    at TestInsert.main(TestInsert.java:39)

问题1.请告诉我如何生成参数:authToken,以及authToken和accessToken之间的区别是什么?

问题2.我的测试代码有什么问题?我什么时候能得到上面的例外。

1 个答案:

答案 0 :(得分:0)

authToken是您可以自己定义的任意String。另一方面,accessToken是在身份验证过程中提供给您的令牌。它会传递给Google Mirror API,以便您的应用程序可以访问用户数据。

请参阅this帖子的选定答案。