繁体   English   中英

谷歌云 Function:Python 和 CORS

[英]Google Cloud Function: Python and CORS

谁能告诉我我做错了什么,我一直在阅读 Google Cloud Functions 文档,但这对我来说没有意义......

这是文档的链接: https://cloud.google.com/functions/docs/writing/http#functions_http_cors-python

这是代码:

import flask, json, logging, os, requests
from requests.exceptions import HTTPError

def get_accesstoken():
  try:
    headers   = {'Content-type': 'application/json', 'Accept': 'text/plain'}
    authUrl   = f"{os.environ.get('AUTH_BASE_URI')}{os.environ.get('AUTH_ENDPOINT')}"
    payload   = {'client_id': os.environ.get("CLIENT_ID"), 'client_secret': os.environ.get("CLIENT_SECRET"), 'grant_type': os.environ.get("GRANT_TYPE")}
    resp      = requests.post(authUrl, data=json.dumps(payload), headers=headers)

    return resp.json()

  except HTTPError as http_err:
    print(f'HTTP error occurred: {http_err}')
    return http_err

  except Exception as err:
    print(f'Other error occurred: {err}')
    return err


def addrecord(request): ## <--- this is the entry cloud function
  """HTTP Cloud Function
  Add records to ANY MC DataExtension
  
  Args:
        request (flask.Request): The request object.
        <http://flask.pocoo.org/docs/1.0/api/#flask.Request>
    ----------------------------------------------------------------------------
    data        = request.get_json().get('data', [{}]) // JSON array of objects
    dataId      = request.get_json().get('dataId', None) // string
  """

  request_json  = request.get_json(silent=True)
  token         = get_accesstoken()
  payload       = request_json["data"]
  dextUrl       = f"{os.environ.get('REST_BASE_URI')}{os.environ.get('REST_DE_ENDPOINT')}{request_json['dataExtId']}/rowset"

  # Set CORS headers for the preflight request
  if request.method == 'OPTIONS':
      # Allows GET & POST requests from any origin with the Content-Type
      # header and caches preflight response for an 3600s
      headers = {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Methods': 'GET, POST',
          'Access-Control-Allow-Headers': 'Content-Type',
          'Access-Control-Max-Age': '3600'
      }

      return ('', 204, headers)

  headers       = {
    'Content-type': 'application/json',
    'Authorization': 'Bearer '+token["access_token"],
    'Access-Control-Allow-Origin': '*'
  }

  resp          = requests.post(dextUrl, data=json.dumps(payload), headers=headers)
  return(resp.raise_for_status(), 200, headers)

当我尝试从前端表单发送 POST 请求时 - 我收到以下错误:

Access to XMLHttpRequest at 'https://xxxxxxxxxx.cloudfunctions.net/addrecord' from origin 'https://mywebsite.com' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

老实说,我不明白我错过了什么/做错了什么……我也觉得我可能把事情复杂化了。

为了完成这个循环,这里是 POST 请求的 JS 代码:

let postData = {...};

$.ajax({
        url: 'https://xxxxxxxxxx.cloudfunctions.net/addrecord',
        type: 'post',
        crossDomain: true,
        contentType: 'application/json',
        dataType: 'json',
        data: JSON.stringify(postData),
        success: function (data) {
          console.info(data);
        },
        error: function (data) {
          console.log(data)
        }
      });

if else

所以你的第二个headers = block 总是那个被设置的。 第二个分配不是将这些标头附加到数据中,而是完全重新分配变量。 所以你没有在那里得到访问源头。

测试它以验证的方法是在第二次分配之后放置一个print(headers)以查看发生了什么。

编辑:在 OPTIONS 案例的 if 块中缺少返回。

感谢@Gabe Weiss——

我意识到我需要做三件事......

首先,我在if request.method == 'OPTIONS':语句的末尾添加了return ('', 204, headers)

其次,我将请求调用移至设置标题后。 最后,我返回了响应

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM