waleedAhmad1 / google-glass-api

Automatically exported from code.google.com/p/google-glass-api
0 stars 0 forks source link

GDK mirror.accounts.insert().. issue. #657

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago

I was testing the authentication for my GDK Glassware : This is my code:

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

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();
      }
    }
}

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

But I get an exception:
==========================================================================

   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)
==========================================================================
2.what's the problem with my test code ?

Original issue reported on code.google.com by lensZh...@gmail.com on 5 Jan 2015 at 2:58