0

我正在android上开发一个应用程序。我的应用程序使用 Google Calendar Api,现在我想包含 Google Drive Api,所以我更改了一些库。而且我的日历服务生成器有一行已弃用的代码,这是:

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

但它从不显示日志。

4

1 回答 1

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)
于 2012-09-04T18:49:43.257 回答