未经身份验证写入 Google 个工作表
Writing to Google Sheets without authentication
在我的一个项目中,我需要将应用程序(iOS 和 Android)中的数据写入 Google Sheet 文档。我需要将数据写入不属于用户的点差sheet,点差sheet在我公司账户下。
根据文档,对于这种情况,我可以使用 Create credentials > API key 来设置none-身份验证密钥。
我担心的是任何人都可以在应用程序中获取此密钥(使用一些基本的逆向工程方法)。我有一个想法创建一个用户 write only 凭据并使用他的 API-KEY 来执行 API要求。但我确信有更好的方法来实现这一目标。
我有哪些选择?非常感谢!
您可以使用 google 应用程序脚本来部署任何人甚至匿名都可以访问的网络应用程序,然后在您的应用程序中创建一个运行该脚本的异步方法。
可以在此处看到此类 webapp 的示例:https://gist.github.com/petyr47/7af08ec2d982399c1b4ace2734597d3b.js
然后你可以添加一个 AsyncTask 方法,如下所示:
public class SendRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try{
//Enter script URL Here
URL url = new URL("Your App Script Web App URL");
JSONObject postDataParams = new JSONObject();
//Passing scanned code as parameter
postDataParams.put("sdata",scannedData);
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}
您可以使用以下方法执行此方法:
new SendRequest().execute();
将方法中的参数替换为您要发送的内容。
希望这有帮助
虽然这是 android 的解决方案,但我无法帮助 iOS,但您可以应用相同的原则。
在我的一个项目中,我需要将应用程序(iOS 和 Android)中的数据写入 Google Sheet 文档。我需要将数据写入不属于用户的点差sheet,点差sheet在我公司账户下。
根据文档,对于这种情况,我可以使用 Create credentials > API key 来设置none-身份验证密钥。
我担心的是任何人都可以在应用程序中获取此密钥(使用一些基本的逆向工程方法)。我有一个想法创建一个用户 write only 凭据并使用他的 API-KEY 来执行 API要求。但我确信有更好的方法来实现这一目标。
我有哪些选择?非常感谢!
您可以使用 google 应用程序脚本来部署任何人甚至匿名都可以访问的网络应用程序,然后在您的应用程序中创建一个运行该脚本的异步方法。 可以在此处看到此类 webapp 的示例:https://gist.github.com/petyr47/7af08ec2d982399c1b4ace2734597d3b.js 然后你可以添加一个 AsyncTask 方法,如下所示:
public class SendRequest extends AsyncTask<String, Void, String> {
protected void onPreExecute(){}
protected String doInBackground(String... arg0) {
try{
//Enter script URL Here
URL url = new URL("Your App Script Web App URL");
JSONObject postDataParams = new JSONObject();
//Passing scanned code as parameter
postDataParams.put("sdata",scannedData);
Log.e("params",postDataParams.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader in=new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuffer sb = new StringBuffer("");
String line="";
while((line = in.readLine()) != null) {
sb.append(line);
break;
}
in.close();
return sb.toString();
}
else {
return new String("false : "+responseCode);
}
}
catch(Exception e){
return new String("Exception: " + e.getMessage());
}
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(getApplicationContext(), result,
Toast.LENGTH_LONG).show();
}
}
public String getPostDataString(JSONObject params) throws Exception {
StringBuilder result = new StringBuilder();
boolean first = true;
Iterator<String> itr = params.keys();
while(itr.hasNext()){
String key= itr.next();
Object value = params.get(key);
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(key, "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(value.toString(), "UTF-8"));
}
return result.toString();
}
}
您可以使用以下方法执行此方法:
new SendRequest().execute();
将方法中的参数替换为您要发送的内容。 希望这有帮助
虽然这是 android 的解决方案,但我无法帮助 iOS,但您可以应用相同的原则。