Google表格错误401,我无法在电子表格上添加/编辑数据 [英] Google sheets Error 401 , I can't add/edit data on spreadsheet

查看:162
本文介绍了Google表格错误401,我无法在电子表格上添加/编辑数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Android应用,该应用可以从Google电子表格&中读取数据从用户输入中获取新值,以在同一电子表格中更新/创建一行或更多行.

I am creating an Android app that reads data from a google spreadsheet & takes new values from user input to update/create a row or more in the same spreadsheet.

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

Actually, within the same app I can successfully access the data from the spreadsheet using GET method, receiving a JSON object and takes the required data using the following code:

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的问题,我正在使用以下代码进行连接:

But, the problem that I get Error 401 when I am trying to write the user data to the same spreadsheet using "POST" method, I am using the following code to connect:

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&吗?如何将数据添加到Google电子表格?

Could you help me why I get error 401 & how can I add my data to a google spreadsheet ?

我已将电子表格的隐私设置为公开&任何人都可以使用链接进行编辑,但出现相同的错误.

I have set my spreadsheet privacy to public & anyone can edit with link but I get the same error.

推荐答案

我希望问题在于您没有通过任何身份验证. 401是未经授权"的响应.如果电子表格是公开的,则未经授权的读取可能会下降,但是即使电子表格是公开的,从API进行的写入也需要授权.

I expect the problem is that you're not passing any authentication. A 401 is an "UNAUTHORIZED" response. Reads can be down without authorization if the spreadsheet is public, but writes from the API require authorization, even if the spreadsheet is public.

您应该考虑使用 Sheets API客户端库,它可以简化为您工作并简化身份验证.

You should consider using the Sheets API client libraries, which simplify the work for you and make authentication easier.

这篇关于Google表格错误401,我无法在电子表格上添加/编辑数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆