What scope is needed to use the findReplace function in GoogleSheets API v4? - google-sheets-api

I want to use the findReplace request in the Google Sheets APIv4. I set up my Scope and request as defined in the Google quickstart guide for the Sheets API in Python and have confirmed that the API can talk to my spreadsheet.
https://developers.google.com/sheets/api/reference/rest/v4/spreadsheets/request#FindReplaceRequest
SCOPES = ['https://www.googleapis.com/auth/spreadsheets']
SHEET_ID = myspreadsheetid
creds = None
store = file.Storage('sheets_token.json')
if not creds or not creds.valid:
if creds and creds.expired and creds.refresh_token:
creds.refresh(Request())
else:
flow = client.flow_from_clientsecrets('client_2_https.json', SCOPES)
creds = tools.run_flow(flow, store, http=Http(disable_ssl_certificate_validation=True))
with open('sheets_token.pickle', 'wb') as token:
pickle.dump(creds, token)
adminService = build('sheets', 'v4', http=creds.authorize(Http(disable_ssl_certificate_validation=True)))
def findreplace_request(find, replacement):
findreplace_request = {}
findreplace_request['find'] = find
findreplace_request['replacement'] = replacement
findreplace_request['matchCase'] = True
findreplace_request['matchEntireCell'] = True
findreplace_request['searchByRegex'] = False
findreplace_request['includeFormulas'] = False
findreplace_request['sheetId'] = mysheetid
allSheets = False
request = {}
request['findReplace'] = findreplace_request
return request
body = {}
body.setdefault('requests',[]).append(findreplace_request('#mydate#','TODAY'))
response = adminService.spreadsheets().batchUpdate(spreadsheetId=SHEET_ID, body=my_request).execute()
I clearly set a scope to read and write to/from Google Sheets, but I do not understand why I get an error that says the scope is not set.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\wnh659\AppData\Local\Continuum\anaconda3\lib\site-packages\googleapiclient\_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Users\wnh659\AppData\Local\Continuum\anaconda3\lib\site-packages\googleapiclient\http.py", line 851, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://sheets.googleapis.com/v4/spreadsheets/1Sx0CJJo-b6Z6JUaQEQ6cJ3Yxtjv3z9BtHN9EHl0-0jU:batchUpdate?alt=json return
ed "Invalid requests[0].findReplace: scope not set.">

You want to use the findReplace request of Sheets API using python.
You have already been able to modify Spreadsheet using Sheets API.
If my understanding is correct, how about this modification? I think that the reason of the error of Invalid requests[0].findReplace: scope not set. is that the scope for replacing the value is not defined. In your request body, the properties of range, sheetId and allSheets are not used. So how about the following modification?
Modified script:
From:
def findreplace_request(find, replacement):
findreplace_request = {}
findreplace_request['find'] = find
findreplace_request['replacement'] = replacement
findreplace_request['matchCase'] = True
findreplace_request['matchEntireCell'] = True
findreplace_request['searchByRegex'] = False
findreplace_request['includeFormulas'] = False
sheetId = mysheetid
allSheets = False
request = {}
request['findReplace'] = findreplace_request
return request
To:
def findreplace_request(find, replacement):
findreplace_request = {}
findreplace_request['find'] = find
findreplace_request['replacement'] = replacement
findreplace_request['matchCase'] = True
findreplace_request['matchEntireCell'] = True
findreplace_request['searchByRegex'] = False
findreplace_request['includeFormulas'] = False
findreplace_request['sheetId'] = mysheetid # Added
allSheets = False
request = {}
request['findReplace'] = findreplace_request
return request
In this modification, find is searched from the sheet of mysheetid. If you want to search the value from all sheets, please use the property of allSheets instead of sheetId.
Note:
If the value of property is boolean, when the property is not used, the value is used as False of the default value.
In your script, I think that the following modification can be also worked.
body = {}
body.setdefault('requests',[]).append(findreplace_request('#mydate#','TODAY'))
response = adminService.spreadsheets().batchUpdate(spreadsheetId=SHEET_ID, body=body).execute()
Reference:
FindReplaceRequest
If I misunderstood your question and this was not the result you want, I apologize.

Related

Can I bind a Lambda Layer directly to a static ARN instead of a zip file

I want to use an AWS provided Layer in a Lamba function. In Terraform what is the preferred way to bind it? Also, can the ARN be bound directly to the Layers property of the module, bypassing the need for defining the layer?
resource "aws_lambdas_layer" "lambda_layer"{
#filename = "python32-pandas.zip"
layer_name= "aws-pandas-py38-layer"
arn = "arn:aws:lambda:us-east-1:xxxxxx:AWSSDKPandas-Python38:1" #? Is this valid
}
module "lambda_test" {
source = "git::https://git.my-custom-aws-lambda.git"
application = var.application
service = "${var.service}-test"
file_path = "lambda_function.zip"
publicly_accessible = false
data_classification = "confidential"
handler = "lambda_function.lambda_handler"
runtime = "python3.8"
tfs_releasedefinitionname = ""
tfs_releasename = "0"
vpc_enabled = true
vpc_application_tag = "aws-infra"
promote = true
rollback = false
create_cwl_group = true
cwl_prefix = "my-project"
create_cwl_subscription = false
#Could layers an arn?
layers = [aws_lambda_layer_version.lambda_layer.arn]
timeout = 600 ####10 mins
memory_size = 1024 #### 1GB
environment = {
variables = {
destination_bucket_name = "us-east-1-my-sbx-${terraform.workspace}"
}
}
}
Doh! The layers property is an [array]. Minor lapse of reading comprehension on my part :/
The solution is to bind the layers to an array of ["arns"] pointing to the aws or custom arn(s).
layers = ["arn:aws:lambda:us-east-1:336392948345:layer:AWSSDKPandas-Python39:1"]

Roblox - Call external GraphQL API

I would like to call an external graphql API (without authentication for the moment).
Here is my code :
local open_api = "https://graphqlzero.almansi.me/api"
local payload = '{"query": "query { post(id: 1) { id title body }}"}'
local headers = {
}
local function craftCall()
local response
local data
pcall(function ()
response = HttpService:PostAsync(open_api, payload, Enum.HttpContentType.ApplicationJson, false, headers)
data = HttpService:JSONDecode(response)
end)
if not data then return false end
print(data)
return false
end
if craftCall() then
print("Success")
else
print("Something went wrong")
end
I get always something went wrong. I need some help on what is going wrong... Specially I don't know if am I correctly formatting the Payload.
After your http call, you never return a success result. You've only outlined failure cases :
if not data then return false end
print(data)
return false
So your conditional, if craftCall() then always evaluates to false.
Why not make it return true or data after the print(data)? Then you'll know that it made it to the end of the call successfully.
local function craftCall()
local success, result = pcall(function()
local response = HttpService:PostAsync(open_api, payload, Enum.HttpContentType.ApplicationJson, false, headers)
return HttpService:JSONDecode(response)
end)
if not success then
warn("PostAsync failed with error : ", result)
return false
end
-- return the parsed data
return result
end

Kong plugin do not run access block

I'm developing a plugin to Kong API Gateway. I created a service pointing it to another service in the local network and basically every request to my service is redirected to the other one, so far so good.
What the plugin has to do is grab the field Authorization Bearer in the header, and pass to the upstream service as part of the URI. E.g.
Request is received on:
localhost/service
In its header, it have a Authorization Bearer that contains a JWT
The plugin has to receive it, take the JWT and parse it to URI to the upstream service:
productionServer/service/9a8udoadzlkndid813gru1gr <-JWT took from header
My attempt till now:
local singletons = require "kong.singletons"
local BasePlugin = require "kong.plugins.base_plugin"
local responses = require "kong.tools.responses"
local constants = require "kong.constants"
local multipart = require "multipart"
local cjson = require "cjson"
local url = require "socket.url"
local access = require "kong.plugins.ctk.access"
local CtkHandler = BasePlugin:extend()
CtkHandler.PRIORITY = 3505
CtkHandler.VERSION = "0.1.0"
file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
io.input(file)
file:write("--- JUST EXTENDED THE BASE PLUGIN ---")
function CtkHandler:new()
CtkHandler.super.new(self, "ctk")
file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
io.input(file)
file:write("--- INSTACIATED ITSELF ---")
end
function CtkHandler:access(conf)
CtkHandler.super.access(self)
file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
io.input(file)
file:write("--- STARTED THE ACCESS PART ---")
do_authentication()
access.execute(conf)
end
file:close()
return CtkHandler
The idea, is that after every request, the access block at the end be executed, then, he will redirect to my access file
local singletons = require "kong.singletons"
local BasePlugin = require "kong.plugins.base_plugin"
local responses = require "kong.tools.responses"
local constants = require "kong.constants"
local multipart = require "multipart"
local cjson = require "cjson"
local url = require "socket.url"
local basic_serializer = require "kong.plugins.log-serializers.basic"
local string_format = string.format
local ngx_set_header = ngx.req.set_header
local get_method = ngx.req.get_method
local req_set_uri_args = ngx.req.set_uri_args
local req_get_uri_args = ngx.req.get_uri_args
local req_set_header = ngx.req.set_header
local req_get_headers = ngx.req.get_headers
local req_clear_header = ngx.req.clear_header
local req_set_method = ngx.req.set_method
local ngx_decode_args = ngx.decode_args
local ngx_re_gmatch = ngx.re.gmatch
local string_format = string.format
local cjson_encode = cjson.encode
local ipairs = ipairs
local request = ngx.request
local function retrieve_token(request, conf)
file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
io.input(file)
file:write("--- RUNNING RETRIEVE TOKEN ---")
local uri_parameters = request.get_uri_args()
for _, v in ipairs(conf.uri_param_names) do
if uri_parameters[v] then
return uri_parameters[v]
end
end
local ngx_var = ngx.var
for _, v in ipairs(conf.cookie_names) do
local jwt_cookie = ngx_var["cookie_" .. v]
if jwt_cookie and jwt_cookie ~= "" then
return jwt_cookie
end
end
local authorization_header = request.get_headers()["authorization"]
if authorization_header then
local iterator, iter_err = ngx_re_gmatch(authorization_header, "\\s*[Bb]earer\\s+(.+)")
if not iterator then
return nil, iter_err
end
local m, err = iterator()
if err then
return nil, err
end
if m and #m > 0 then
return m[1]
end
end
end
local function do_authentication(conf)
file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
io.input(file)
file:write("--- RUNNING DO_AUTHENTICATION ---")
local token, err = retrieve_token(ngx.req, conf)
if err then
return responses.send_HTTP_INTERNAL_SERVER_ERROR(err)
end
local ttype = type(token)
if ttype ~= "string" then
if ttype == "nil" then
return false, {status = 401}
elseif ttype == "table" then
return false, {status = 401, message = "Multiple tokens provided"}
else
return false, {status = 401, message = "Unrecognizable token"}
end
append_uri(token)
return true
end
end
local function append_uri(token)
file = io.open("/usr/local/kong/logs/ctk.lua", "a+")
io.input(file)
file:write("--- FUNCTION APPEND_URL ---")
local uri = ngx.get_uri_args
ngx.req.set_uri(ngx.unescape_uri("/" .. token))
end
In the Kong server, after installing the plugin above, I receive:
--- JUST EXTENDED THE BASE PLUGIN ------ INSTACIATED ITSELF ---
Which is the control inserted inside the code to trace it.
Any ideas?
Actually using io.write isn't recommended, so what i had to do was change it to:
ngx.log(ngx.WARN, "SOME MESSAGE")
After that, the block code access ran just fine.
There's a Kong plugin that can perform the OAuth 2.0 token validation, see: kong-oidc. You may want to deploy that.

Swagger UI doesn't support uploading a file properly for RestEasy

I use a JAX-RS (RestEasy) along with a Swagger. One of my endpoint can upload a file. Defined way to upload the file (in RestEasy) is to provide a org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput as a parameter.
Here is my endpoint:
#PUT
#Path("/apis/{id}/file")
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.APPLICATION_JSON)
#ApiOperation(value = "Registers a file.", code = 201, nickname = "registerFile")
#ApiResponses(
value = {
#ApiResponse(code = 201, message = "File created.",
response = FileCreated.class),
#ApiResponse(code = 400, message = "Invalid parameters."),
#ApiResponse(code = 404, message = "API is not found.")})
Response registerFile(
#ApiParam(value = "API ID.", required = true) #PathParam("id") String apiId,
#ApiParam(value = "File to register.", required = true, type = "file", name = "apiFile")
MultipartFormDataInput apiFile) throws AppException;
What is the problem?
Unfortunately, swagger-ui generates a schema based on the inner properties of the MultipartFormDataInput instead of a button to upload the file.
I tried use a #FormParam annotation (to indicate that the providing parameter should be interpreted as file) along with the MultipartFormDataInput parameter, but then the app doesn't want to compile.
Question: Is there any solution/workaround to provide the button to upload the file in the swagger-ui?
The solution is removing #ApiParam from your apiFile argument and adding #ApiImplicitParam (which is not bound to Jax-RS and allows defining parameters manually) above the method :
#ApiImplicitParams({#ApiImplicitParam (value = "File to register.", required = true, dataType = "file", name = "apiFile", paramType="formData")})
The final solution
The final solution includes a selected answer, but instead of removing #ApiParam we should add #ApiParam(hidden = true). Why?
If we remove #ApiParam, there are two fields: apiId, body with the inner properties of the MultipartFormDataInput and the button to upload the file in the swagger-ui. This body field is a side effect. To fix this issue we should provide #ApiParam(hidden = true), then there are the field with apiId and the button to upload the file in the swagger-ui.
BTW: I tested below code for swagger-ui in 1.5.12 version.
#PUT
#Path("/apis/{id}/file")
#Consumes(MediaType.MULTIPART_FORM_DATA)
#Produces(MediaType.APPLICATION_JSON)
#ApiOperation(value = "Registers a file.", code = 201, nickname = "registerFile")
#ApiResponses(
value = {
#ApiResponse(code = 201, message = "File created.",
response = FileCreated.class),
#ApiResponse(code = 400, message = "Invalid parameters."),
#ApiResponse(code = 404, message = "API is not found.")})
#ApiImplicitParams(
#ApiImplicitParam(value = "File to register.", required = true, dataType = "file",
name = "apiFile", paramType = "formData"))
Response registerFile(
#ApiParam(value = "API ID.", required = true) #PathParam("id") String apiId,
#ApiParam(hidden = true) MultipartFormDataInput apiFile) throws AppException;

hidden variable in R

I am using the RLastFM package, and have some question about the function:
> tag.getTopArtists
function (tag, key = lastkey, parse = TRUE)
{
params = list(method = "tag.gettopartists", tag = tag, api_key = lastkey)
ret = getForm(baseurl, .params = params)
doc = xmlParse(ret, asText = TRUE)
if (parse)
doc = p.tag.gettopartists(doc)
return(doc)
}
the author included lastkey as the api_key, but I can't find it by using ls(), where is it?
Thanks.
getAnywhere(lastkey) show you where it is and RLastFM:::lastkey gives you this value. The value isn't exported from namespace of package.
For more details check Writing R Extensions manual, Package name spaces section.