回答

收藏

OAuth,OAuthConfig和Google Apps脚本(融合表API-SQL插入问题)

技术问答 技术问答 133 人阅读 | 0 人回复 | 2023-09-13

我正在开发一个Google9 ^1 y3 F) k1 \6 k* {, W
Apps脚本函数,它将在融合表中插入一行。我的大多数融合表接口都能正常工作,但这是我第一次尝试使用sql’POST’查询。根据此处的文档,我应该能够将sql语句放入POST正文中。我将自己简化为OAuth
# R2 j. a0 c9 W$ Y7 ZPlayground进行故障排除,并且不断收到以下错误。我一直在尝试使用sql$ ^3 N, y; h* A" Q) j, q6 b
insert语句,sql=INSERT INTO {fusionId} (HeadingName) VALUES3 x" d4 V( w; ^( g
(ValueOne)并且尝试了该语句的各种变体但无济于事。请帮助我确定执行此操作的正确语法或方法。
! y2 {) i- D# G8 W8 Y2 Y& i9 a{+ n& c; p: n4 e* j' c: N4 w
"error": {' ^3 E6 D' h. B6 U1 t
  "errors": [) Y1 X: Y5 b- D1 y7 |5 o
   {$ r/ B" W! Z% v+ E; U
    "domain": "global",8 W2 o/ `( o+ O, P6 K; [
    "reason": "required",% Y) A) r2 V8 f, G6 M1 Q  [
    "message": "Required parameter: sql",; Y# c0 p" ^9 p
    "locationType": "parameter",' c0 u; [4 F) c4 n8 a8 k
    "location": "sql"7 b! i4 w, a& T4 a1 M
   }
' h4 b4 j0 q+ C- \3 g  ],
) D( D& L% z( d7 Y) o* i6 v  "code": 400,
  Q2 v0 x) F6 z  "message": "Required parameter: sql": |7 G4 n& G' z0 x# n
}
/ Q, Z$ l5 @$ m/ R! Y( `: q}
+ v+ e7 T+ I* |1 d7 g+ F一旦弄清楚了,我就需要在我的代码中重复执行Playground操作,(我认为)看起来像这样:
  n& A6 G, c, J* }5 Ffunction fusionRequest(methodType, sql, oAuthFields) {
* _; o# `3 ]" U9 o9 p; l  OAL.googleAuth(oAuthFields);' [" M- P) ]% v
  var options =
0 r% ^+ v5 R5 Y& Z' b    {  V( g% V- M5 p$ D% L& L: M
      oAuthUseToken : "always",
4 T3 W, b0 J- W( b: w9 s* X: V      oAuthServiceName : 'fusiontables',! p) E0 @/ L+ C
      method : &quotOST",( U' O, J8 T1 Q2 A% U$ I
      payload : "sql=INSERT INTO {fusionId} (Heading) VALUES (ONE)",+ i1 |% {$ k8 C- f# _2 g
      contentType : "application/json"
" u& J0 x, z* W4 k1 ]: s" V/ x    };6 m0 O% H# C# \  H  E
Logger.log(options)
( p) t  ]- f: V$ i  var fetchResult = UrlFetchApp.fetch(oAuthFields.queryUrl, options);
. a! x1 b# ?8 q2 K3 N' N/ v  return JSON.parse(fetchResult.getContentText());  
1 T' t7 `; E; s. N# C4 n2 g7 R9 y}
1 O2 U4 L1 r5 U- ?' X' F我一直在想,内容类型也可能是个问题,但我很茫然。请帮忙。
/ N! [: ^5 [2 Y4 G' M更新#1 通过使用三亚的建议,我能够使请求在OAuth
8 s8 E% i7 y' {6 [- v' OPlayground中运行。但是,我仍在努力处理代码中的请求。我在运行代码时收到重复的授权请求(或在调试时出现一般的OAuth错误)。从我以前的经验在这里,我相信,这意味着仍然有一些错误与我的有效载荷。任何对此的建议将不胜感激。* A/ R4 g% G: p0 [& g  m: h
var oAuthFields = {/ U* Y$ X, b2 l+ b2 J$ G: X$ A
  'clientId' : '523606257547.apps.googleusercontent.com',5 o2 ~  ~: h( q  K
  'scope' : 'https://www.googleapis.com/auth/fusiontables',
: l1 c& p* P8 q+ u) ~2 Z  'fetchUrl' : 'https://www.googleapis.com/fusiontables/v1/',- H7 y# |8 k! T: e# t0 M
  'clientSecret' : 'L-f8DgwK4rs7Qmw9k5IFL7lZ',
* M) Z/ D8 w$ v4 I* A. j- B  'fusionId' : '1b4kT_aYRfNBy8ZPtSZqhQUqVSVIYj_QWiBmjXXI',' t0 L1 c: H8 x+ v4 [1 t3 ]
  'service' : 'fusiontables',
* P) ~7 h* c7 J/ R) A  'queryUrl' : 'https://www.googleapis.com/fusiontables/v1/query/': p' X! F( j& H( I3 \/ v6 h
};8 Q- ]% m" q/ d0 }' B4 U
function fusionRequest(methodType, sql, oAuthFields) {
5 z+ H/ |, N; c, b  OAL.googleAuth(oAuthFields);
9 R$ K2 |- M1 O) c2 N7 S8 A/ L- q  var options =
2 z0 Z$ I: p* c    {2 B7 k( V- ~; a! T/ p; [$ U6 D9 ~% H
      oAuthUseToken : "always",
- ]( B0 O6 ~0 }# c- A      oAuthServiceName : 'fusiontables',
7 j$ M, p7 c  M      method : &quotOST",, o& l) ]4 c' V8 F& N% u9 M
      payload : "sql=INSERT INTO {fusion id} (\'Heading\') VALUES (\'ONE\')",
' t4 t" g$ m: O; ?      contentType : "application/x-www-form-urlencoded"
3 n% g# v) ^4 g% a, `$ C* s2 H! [) W3 W/ z    };/ l2 l- M% q" k# a& I& g" i
  var fetchResult = UrlFetchApp.fetch(oAuthFields.queryUrl, options);! {! c, d+ Q* |! ?
  return JSON.parse(fetchResult.getContentText());  
$ ]9 z7 C: C- C}' M0 I% y1 q' Y' W  }
对于上下文,googleAuth()函数和此函数的总体布局与我用于在融合表中添加列的函数相同(有效)。
- q4 m) U2 d5 B2 Y  G: X$ s8 I. }                - a- P; Q- ~( d9 M
解决方案:
& U' s7 ^: T" W% b4 v  ^               
1 W8 g8 [0 k0 E
& A. R$ J3 a; x  ]  R# G1 j& `
8 E) e7 L3 T& \5 c/ i$ m4 D                成功!经过大量研究,我已经解决了这个问题。这不是有效载荷或contentType的问题(实际上,我发现.fetch自动默认为“ application /3 {) w9 p7 Z6 u0 O9 |) c3 C2 Z
x-www-form-urlencoded”编码)。问题出在授权上。我使用James Ferreira的Fusion( \5 J) }' K: k
Table库作为模型,并在代码和库的Logger.log(UrlFetchApp.getRequest(url,1 j4 L$ B* @! G% m: k
fetchArgs))之前插入了该表UrlFetchApp.fetch(url, fetchArgs).getContentText()。& W+ z  O5 u; h& z) j
//log entry for library8 \" }9 ~: P  ]2 x# B7 ^# R
{oAuthServiceName=fusion, useIntranet=false, followRedirects=true, oAuthUseToken=always, payload=sql=INSERT INTO 1b4kT_aYRfNBy8ZPtSZqhQUqVSVIYj_QWiBmjXXI ('Heading', 'Heading 2') VALUES ('NONE','TWO'), method=POST, validateHttpsCertificates=true, contentType=application/x-www-form-urlencoded, url=https://www.google.com/fusiontables/api/query}
( x1 L! U' j) T8 X% c//log entry for my code
% o/ L4 P9 [( T{oAuthServiceName=fusiontables, useIntranet=false, followRedirects=true, oAuthUseToken=always, payload=sql=INSERT+INTO+1b4kT_aYRfNBy8ZPtSZqhQUqVSVIYj_QWiBmjXXI+('Heading',+'Heading+2')+VALUES+('NONE','TWO'), method=POST, validateHttpsCertificates=true, contentType=application/x-www-form-urlencoded, url=https://www.googleapis.com/fusiontables/v1/query}
" O3 R& Z8 S( M. [, [0 }/ O+ c然后,我将工作库的日志与我自己的日志进行了比较,发现三个不同之处。
/ W2 t! J- [! ?/ `[ol]我认为sql语句的编码有所不同(“ INSERT INTO”与“ INSERT + INTO”)。# ~' r2 q4 W; a. w, M9 f
请求的“服务”参数不同(“融合”与“融合表”)。2 D8 R" q2 X$ d/ w* a+ T2 |
qpi查询网址不同。(“ www.google.com/fusiontables/api/query”与“ www.googleapis.com/fusiontables/v1/query”)。
2 @' L5 v5 y: |* N5 X' n& b[/ol]& t* Y4 V; K4 h1 F& v
经过一些实验,我确定…4 o# G9 }+ P- {$ @. d9 w& n: I: {
[ol]假定的编码与我的错误无关。, s* a8 U* l+ R: X3 g! b
服务参数也无关紧要。我在各种文档中都看到过,似乎其中任何一个都行得通。
: l5 k7 W/ H0 f令人沮丧的是,这就是问题所在。令人沮丧的是,因为api文档说要使用一种无??效的方法。显然,www.googleapis.com / fusiontables / v1 / query适用于OAuth2.0。OAuthConfig(我在GAS中使用的临时授权工具)尚未迁移到2.0,因此缺少文档。我已经提交了功能请求,以将OAuthConfig迁移到此处。如果您也想解决这个问题,请给该线程一些爱。" C: T1 p2 ~$ }- K3 n: G8 @- {5 V
[/ol]
分享到:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则