Я хочу прочитать таблицу из Google Sheets из моего приложения для Android.
Я хочу сделать это с помощью API Google Sheets.
Я объявил лист как открытый, создал ключ API и попытался отправить вызов службы GET
:
https://sheets.googleapis.com/v4/spreadsheets/{My Sheet key}/values/responses:append?key={My API credential key}
Я получаю код 401.
Ответ:
В запросе отсутствуют необходимые учетные данные для аутентификации. Ожидаемый OAuth
2 токен доступа, логин cookie или другие действительные учетные данные для аутентификации.
Увидеть
https://developers.google.com/identity/sign-in/web/devconsole-project.
Мой код:
private static final String SHEET_URL = "https://sheets.googleapis.com/v4/spreadsheets/1d534sQ5xaNbr65wMM_qH2yjXo3EPrrp3o34z-Foledg/values/responses:append?key=AIzaSyDT88Nq6jhtaKH-vIVEuvGO1d9Sx8ewR0w";
public String GetConanimList() throws Exception {
URL url = new URL(SHEET_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-Type", "application/json");
OutputStream os = conn.getOutputStream();
String jsonPayload = null;
//os.write(jsonPayload.getBytes());
os.flush();
os.close();
int statusCode = conn.getResponseCode();
System.out.println("Response from WA Gateway: \n");
System.out.println("Status Code: " + statusCode);
BufferedReader br = new BufferedReader(new InputStreamReader(
(statusCode == 200) ? conn.getInputStream() : conn.getErrorStream()
));
String output;
String response = "";
while ((output = br.readLine()) != null) {
response = response + output;
}
conn.disconnect();
return response;
}
Что мне не хватает? Спасибо.