2017-02-23 65 views
0

我创建一个Android应用程序从一个谷歌电子表格&需要从用户输入新的值更新/在同一个电子表格创建行或更多的数据读取/编辑数据。谷歌片401错误,我不能添加在电子表格

实际上,相同的应用程序内我可以成功地从电子表格使用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(); 
     } 

但是,问题,我

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 &如何:搞定了,我现在用的是下面的代码连接401错误,当我试图写的用户数据使用“POST”方法相同的电子表格我将我的数据添加到goo gle电子表格?

我已经把我的电子表格隐私公开&任何人都可以链接编辑,但我得到了同样的错误。

+0

可以粘贴401响应的全部内容? –

回答

0

我想到的问题是,你不传递任何身份验证。 401是“UNAUTHORIZED”响应。如果电子表格是公开的,则未经授权就可能关闭阅读,但即使电子表格是公开的,API的写入也需要授权。

你应该考虑使用Sheets API client libraries,这简化您的工作,使认证更容易。