This is the test code for authentication and insert Account :
import java.io.IOException;
import java.net.URISyntaxException;
import java.security.GeneralSecurityException;
import java.util.Arrays;
import java.util.Collection;
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.Mirror.Accounts.Insert;
import com.google.api.services.mirror.model.Account;
import com.google.api.services.mirror.model.AuthToken;
public class InsertAccountWithJava {
private static final String SERVICE_ACCOUNT_EMAIL ="540223414844-tqpuqtbv07klpuuk3q04qha0g88094uu@developer.gserviceaccount.com";
private static final String SERVICE_ACCOUNT_PKCS12_FILE_PATH ="C:/Users/Yuan/Desktop/eyenotes-db176c40f51f.p12";
private static final String ACCOUNT_TYPE = "com.eyenotes";
private static final Collection<String> MIRROR_ACCOUNT_SCOPES =Arrays.asList("https://www.googleapis.com/auth/glass.thirdpartyauth");
private static String userToken = "6164da1732ea09b4";
private static String accountName="zhongmei";
private static String authTokenType ="eyenotes";
private static String authToken="myauthtoken";
public static void main(String[] args) {
try {
Mirror mirror = getMirrorService();
createAccount(mirror, userToken, accountName, authTokenType, authToken);
} catch (GeneralSecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
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;
}
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("zmyl123456");
Insert insert = mirror.accounts().insert(userToken, ACCOUNT_TYPE, accountName, account);
insert.execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
============================================================================
I got the exception below:
============================================================================
Fabruary 06, 2015 1:42:21 pm com.google.api.client.googleapis.services.AbstractGoogleClient <init>
warning: Application name is not set. Call Builder#setApplicationName.
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 com.ccfsz.service.InsertAccountWithJava.createAccount(InsertAccountWithJava.java:81)
at com.ccfsz.service.InsertAccountWithJava.main(InsertAccountWithJava.java:30)
=====================================================================================
Parameters explaination:
SERVICE_ACCOUNT_EMAIL:Generated when i create new Client ID in developer console.
SERVICE_ACCOUNT_PKCS12_FILE_PATH:Generated when i create new Client ID in developer console.
ACCOUNT_TYPE:I provided to google when submit my application.
userToken:I got from the authentication url provided to google.
accountName,authTokenType and authToken is arbitrary String.
2.Before i execute the code above ,do i need to do anything else?
3.I can get google authorization page through (https://accounts.google.com/o/oauth2/auth?response_type=code&scope=https://www.googleapis.com/auth/glass.timeline+https://www.googleapis.com/auth/userinfo.email+https://www.googleapis.com/auth/userinfo.profile&access_type=offline&approval_prompt=force&include_granted_scopes=true&client_id=540223414844-jjhqevuu0rhmoa3u6gkd05g7v29i89kk.apps.googleusercontent.com&redirect_uri=http://www.eyenotes.com/glass/login.php) using web application .how can i get this page and get permission using service account.
4.Who can give me any hint ? Sometimes,after execute test code above,i got nothing.in this case,according to their source code , this means i got a Http response code between 200 and 300, but i never get Accounts on my glass device which accountType equals "com.eyenotes".sometimes it return 400 with the excepiton above.
who have any idea,please tell me.I am very appreciate.