【问题标题】:Google Glass GDK authentication using Java使用 Java 的 Google Glass GDK 身份验证
【发布时间】:2014-12-26 01:20:35
【问题描述】:

我正在测试我的 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.我的测试代码有什么问题?我什么时候可以得到上面的异常。

【问题讨论】:

    标签: authentication google-glass google-gdk


    【解决方案1】:

    authToken 是您可以自己定义的任意字符串。另一方面,accessToken 是在身份验证过程中给您的令牌。它被传递到 Google Mirror API 以使您的应用程序能够访问用户数据。

    参考this帖子的选定答案。

    【讨论】:

    • 感谢您的回答。
    • 在您之前评论中提到的链接中,authToken 被定义为“您的服务生成的令牌”。因此,它可以是您选择使用的任何字符串。
    • 好的,谢谢.. 但是我上面的代码有什么问题。我在这里卡了好几天了。
    • 您是否通过了审核流程?您只能在审核后进行测试。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多