smartcar / java-sdk

Java client SDK for the Smartcar API.
https://smartcar.github.io/java-sdk
MIT License
18 stars 20 forks source link

getAccessToken() returns null #9

Closed RobinJayaswal closed 7 years ago

RobinJayaswal commented 7 years ago
try {
      ... get code ...

      System.out.println(code);
      Access access = client.exchangeCode(code);
      System.out.println(access.getAccessToken());
      Api.Vehicles vehicles = client.getVehicles(access.getAccessToken());

} catch (Exception e) {
     System.out.println(e);
}

This prints out

f7583f02-3a1c-4539-9a69-285799abf6f7
null
com.smartcar.sdk.Exceptions$AuthenticationException: {"error":"authentication_error","message":"Invalid or expired token provided."}
RobinJayaswal commented 7 years ago

Fixed, . The problem was with gson.fromJson(json, Access.class).

The json string has property names that are lower case with underscores, and gson looks for exact matches to the constructor parameters, which are camel case. You just have to configure gson to expect this capitalization:

private final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();

Pr #10 fixes.