Android 谷歌表单错误401,我可以';t在电子表格上添加/编辑数据

Android 谷歌表单错误401,我可以';t在电子表格上添加/编辑数据,android,google-sheets-api,Android,Google Sheets Api,我正在创建一个Android应用程序,它可以从谷歌电子表格中读取数据,并从用户输入中获取新值,以更新/创建同一电子表格中的一行或多行数据 实际上,在同一个应用程序中,我可以使用GET方法成功访问电子表格中的数据,接收JSON对象,并使用以下代码获取所需数据: String requestUrl = https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/A1:F200?key={myAPIkey} URL url

我正在创建一个Android应用程序,它可以从谷歌电子表格中读取数据,并从用户输入中获取新值,以更新/创建同一电子表格中的一行或多行数据

实际上,在同一个应用程序中,我可以使用GET方法成功访问电子表格中的数据,接收JSON对象,并使用以下代码获取所需数据:

String requestUrl = https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/A1:F200?key={myAPIkey}
URL url = createUrl(requestUrl);
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
try { urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestMethod("GET");
urlConnection.connect(); } finally {
            urlConnection.disconnect();
        }
但是,当我尝试使用“POST”方法将用户数据写入同一个电子表格时,出现错误401的问题,我使用以下代码进行连接:

String requestUrl = https://sheets.googleapis.com/v4/spreadsheets/{spreadsheetId}/values/A5:F5:append?valueInputOption=USER_ENTERED&key={myAPIkey}

URL url = createUrl(requestUrl);

HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
        try {
            urlConnection.setDoOutput(true);
            urlConnection.setRequestMethod("POST");
            urlConnection.setChunkedStreamingMode(0);

            OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());

            OutputStreamWriter out = new OutputStreamWriter(urlConnection.getOutputStream());
            out.write(object);
            out.flush();
            out.close();
} finally {
            urlConnection.disconnect();
        }
你能告诉我为什么会出现401错误吗&我怎样才能将数据添加到谷歌电子表格中


我已将我的电子表格隐私设置为public&任何人都可以使用link进行编辑,但我收到了相同的错误。

我想问题是您没有通过任何身份验证。401是“未经授权”的响应。如果电子表格是公共的,则可以在未经授权的情况下关闭读取,但从API进行的写入需要授权,即使电子表格是公共的


你应该考虑使用它,这简化了工作并使身份验证更加容易。

你能粘贴401个响应的全部内容吗?