2012-11-15 55 views
3

我正在創建一個應用程序,以在Android上使用Google Drive。該方法setJsonHttpRequestInitializer是未定義的類型Drive.Builder Android

我在網上搜索了幾個例子,但它們都是一樣的。我收到以下錯誤:

Object Drive.Builder does not have method setJsonHttpRequestInitializer. 

我已經使用Google驅動器API V1和V2進行了測試,但運氣不佳。

問題在哪裏?

來源:

private Drive getDriveService(String accountName) { 

    HttpTransport ht = AndroidHttp.newCompatibleTransport();      
    JacksonFactory jf = new JacksonFactory();   
    Credential credential = new Credential(BearerToken.authorizationHeaderAccessMethod()).setAccessToken(accountName);   

     Drive.Builder b = new Drive.Builder(ht, jf, null); 
     b.setJsonHttpRequestInitializer(new JsonHttpRequestInitializer() { 

      @Override 
      public void initialize(JsonHttpRequest request) throws IOException { 
       DriveRequest driveRequest = (DriveRequest) request; 
       driveRequest.setPrettyPrint(true); 
       driveRequest.setOauthToken(accountName); 
       driveRequest.setKey(API_KEY); 
      } 
     }); 
     return b.build(); 
} 

發現,如果使用Google API-Java的客戶機-1.12.0-β

下載和進口谷歌-API的Java的客戶機 - 1.11 setJsonHttpRequestInitializer方法是未定義.0-beta

但是現在得到: 對已安裝的應用程序使用客戶端ID:客戶端ID。

com.google.api.client.http.HttpResponseException: 403 Forbidden 
{ 
    "error": { 
    "errors": [ 
    { 
    "domain": "usageLimits", 
    "reason": "accessNotConfigured", 
    "message": "Access Not Configured" 
    } 
    ], 
    "code": 403, 
    "message": "Access Not Configured" 
    } 
} 

使用簡單API訪問:API密鑰

Exceptioncom.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized 
{ 
    "code" : 401, 
    "errors" : [ { 
    "domain" : "global", 
    "location" : "Authorization", 
    "locationType" : "header", 
    "message" : "Invalid Credentials", 
    "reason" : "authError" 
    } ], 
    "message" : "Invalid Credentials" 
} 

這裏是項目來源:

http://www.sendspace.com/file/an6xxy

什麼是錯的,或許真的有證書指紋(SHA1)?

嘗試從谷歌的calendar-android-sample。

看起來像我在谷歌API控制檯註冊應用程序的問題。

在哪裏把我得到的樣品客戶端ID?

在Android的調試模式下運行示例後,得到「訪問未配置」。

回答

1

您可能正在使用舊版本的Java庫。

無論如何,你可以使用GoogleCredential對象的實例作爲HttpRequestInitializer,如下面的代碼:

private Drive getDriveService(String token) { 
    Credential credential = new GoogleCredential().setAccessToken(token); 
    Drive.Builder b = new Drive.Builder(
     AndroidHttp.newCompatibleTransport(), new JacksonFactory(), credential) 
     .setHttpRequestInitializer(credential); 
    return b.build(); 
} 
+0

是的,這有幫助。但現在得到該訪問未配置。我在Google Drive的Google api控制檯中創建了API KEY,並將其寫入Manifets中,但沒有成功。 Exceptioncom.google.api.client.googleapis.json。GoogleJsonResponseException:403禁止 { 「代碼」:403, 「錯誤」:[{ 「域」: 「usageLimits」, 「消息」: 「訪問未配置」, 「原因」: 「accessNotConfigured」 } ], 「message」:「未配置訪問」 } – user1826780

+0

Drive API不使用API​​密鑰進行授權,而是需要https://developers.google.com/android/google中所述的授權令牌-play-services/authentication –

+0

添加了更多信息,有人可以幫忙嗎? – user1826780

2

在Android上,最好的做法是使用谷歌進行的OAuth 2.0播放服務。從庫的版本1.12.0-beta開始,您應該使用GoogleAccountCredential來完成此操作。

請使用我編寫的示例calendar-android-sample作爲Android上OAuth 2.0的最佳實踐指南。請仔細閱讀說明。請注意,您必須先在Google APIs Console中註冊您的應用程序才能運行。來自樣本的代碼片段:

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    ... 
    // Google Accounts 
    credential = GoogleAccountCredential.usingOAuth2(this, CalendarScopes.CALENDAR); 
    SharedPreferences settings = getPreferences(Context.MODE_PRIVATE); 
    credential.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null)); 
    // Calendar client 
    client = new com.google.api.services.calendar.Calendar.Builder(
     transport, jsonFactory, credential).setApplicationName("Google-CalendarAndroidSample/1.0") 
     .build(); 
} 
+0

嘗試爲日曆示例創建客戶端ID,但獲取:發生意外錯誤。我們正在研究它。這是暫時的谷歌? – user1826780

+1

發現它發生是因爲包名太長。 – user1826780

+0

嘗試日曆示例,在哪裏設置我在谷歌api控制檯中創建的客戶端ID? – user1826780

相關問題