我想从我的android应用程序的Google中读取一张表格。我想通过来做到这一点。我将该表声明为公共的,创建了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;
}我遗漏了什么?谢谢。
发布于 2018-11-20 08:56:58
要使用Google,您需要授权您的请求。
识别应用程序有两种方法:使用OAuth 2.0令牌 (也授权请求)和/或使用应用程序的API密钥。下面是如何确定要使用哪些选项:
您还可以参考可用的速效项目,作为如何正确实现此功能的指南。
https://stackoverflow.com/questions/53371996
复制相似问题