2014-11-24 88 views
0

我想訪問谷歌日曆api使用其java/android API(我遵循例如:http://samples.google-api-java-client.googlecode.com/hg/calendar-android-sample/instructions.html)。訪問未配置爲Android日曆API

我做的是以下幾點:

private static final List<String> SCOPES = Arrays.asList(CalendarScopes.CALENDAR, 
              CalendarScopes.CALENDAR_READONLY); 


private HttpTransport httpTransport = AndroidHttp.newCompatibleTransport(); 
private JsonFactory jsonFactory = GsonFactory.getDefaultInstance(); 
private GoogleAccountCredential credential; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    credential = GoogleAccountCredential.usingOAuth2(this, SCOPES); 
    user = (User) getIntent().getExtras().getSerializable(User.KEY); 
    credential.setSelectedAccountName(user.getEmail()); 
    // user.getEmail() is the value I previously retrieved from the selected 
    // android.accounts.Account.name 

    Calendar cal = new Calendar.Builder(httpTransport, jsonFactory, credential) 
        .setApplicationName("TestApp/1.0") 
        .build(); 
} 

當我後來打電話想這樣做是爲了獲取calendarlist:

String pageToken = null; 
do { 
    CalendarList calendarList = cal.calendarList() 
           .list() 
           .setPageToken(pageToken) 
           .execute(); 
    calendars.addAll(calendarList.getItems()); 
    pageToken = calendarList.getNextPageToken(); 
} while (pageToken != null); 

我得到一個錯誤:

{ 
    "code": 403, 
    "errors": [ 
    { 
     "domain": "usageLimits", 
     "message": "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration.", 
     "reason": "accessNotConfigured", 
     "extendedHelp": "https://console.developers.google.com" 
    } 
    ], 
    "message": "Access Not Configured. The API is not enabled for your project, or there is a per-IP or per-Referer restriction configured on your API key and the request does not match these restrictions. Please use the Google Developers Console to update your configuration." 
} 

任何人都可以幫我解決這個問題嗎?謝謝!

+0

轉到api控制檯,並啓用該項目的API訪問 – njzk2 2014-11-24 22:35:50

+0

@ njzk2我在那裏有一些項目。我怎麼知道我需要啓用api的項目? – mvieghofer 2014-11-24 23:03:39

+0

在您的教程中,您是否按照標有「註冊您的應用程序」的部分? – njzk2 2014-11-24 23:04:48

回答

0

我發現了錯誤。我沒有使用API​​密鑰的完整軟件包名稱。在確認API-Key中使用的軟件包名稱與AndroidManifest.xml中定義的軟件包名稱相同後,它就可以工作了。