我正在开发一个Android应用程序。我的应用程序使用Google Calendar Api,现在我想要包含Google Drive Api,所以我更改了一些libs。我的Calendar Service Builder有一个不推荐使用的代码行,这个:
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accesstoken);
在更改我使用的库之前。:
import com.google.api.client.googleapis.auth.oauth2.draft10.GoogleAccessProtectedResource;
但是现在,我不使用第一行代码,它已被弃用,我需要类似的东西。
这是我的第一个日历构建器服务:
public static Calendar build(String accessToken)
{
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleAccessProtectedResource accessProtectedResource = new GoogleAccessProtectedResource(accessToken);
Calendar service = Calendar.builder(transport, jsonFactory).setApplicationName(Constants.APP_NAME).setJsonHttpRequestInitializer(new JsonHttpRequestInitializer()
{
@Override
public void initialize(JsonHttpRequest request)
{
CalendarRequest calendarRequest = (CalendarRequest) request;
calendarRequest.setKey(Constants.API_KEY);
}
}).setHttpRequestInitializer(accessProtectedResource).build();
return service;
}
}
现在我正在尝试,但这不起作用:
public static Calendar build(TokenResponse tokenResponse)
{
HttpTransport transport = AndroidHttp.newCompatibleTransport();
JacksonFactory jsonFactory = new JacksonFactory();
GoogleCredential credential = new GoogleCredential().setFromTokenResponse(tokenResponse);
Log.w("1","1"+credential);
Calendar service = Calendar.builder(transport, jsonFactory).setApplicationName(Constants.APP_NAME).setJsonHttpRequestInitializer(new JsonHttpRequestInitializer()
{
@Override
public void initialize(JsonHttpRequest request)
{
CalendarRequest calendarRequest = (CalendarRequest) request;
calendarRequest.setKey(Constants.API_KEY);
}
}).setHttpRequestInitializer(credential).build();
return service;
}
}
但它从不显示日志。
答案 0 :(得分:0)
在上面的代码中进行以下更改以使其正常工作
static String authToken;
HttpRequestInitializer requestInitializer = new HttpRequestInitializer() {
public void initialize(HttpRequest request) throws IOException {
request.getHeaders().setAuthorization(GoogleHeaders.getGoogleLoginValue(authToken));
}
};
Calendar service = new Calendar.Builder(transport, jsonFactory, requestInitializer)