Socrata 元数据 API 授权问题

Socrata Metadata API Authorization Issue

我正在尝试使用 Socrata 元数据 API 通过 python 更新我的整个元数据资产清单。使用元数据 Api 文档中的 python 代码示例,我能够读取元数据,但由于错误 403,即使经过适当的身份验证,我也无法更新它。我有管理员访问权限,可以访问秘密令牌和应用程序令牌。我曾尝试同时使用 OAuth2 和基本身份验证,但均无济于事。这是一个快速代码示例:

from urllib2 import Request, urlopen

values = """
  {
    "name": "Business Entities in Colorado_TEST",
  }
"""

headers = {
  'X-Socrata-Host': 'data.colorado.gov',
  'Content-Type': 'application/json',
  'Authorization': 'OAuth {REDACTED}'
}
request = Request('https://data.colorado.gov/api/views/metadata/v1/4eit-nuxn', data=values, headers=headers)
request.get_method = lambda: 'PATCH'

response_body = urlopen(request).read()
print response_body

其中 {REDACTED} 等于 App Token 减去大括号。代替 'Authorization' 我也试过 'X-App-Token' 无济于事。我觉得这是一个非常基本的修复,但我肯定遗漏了一些东西。

我还尝试使用以下方法输入 base64 字符串:

username = '{REDACTED}'
password = '{REDACTED}'
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')

将变量 base64string 添加到我的 headers 和 运行 到同一问题中。基于:

如果有帮助的话,这是我的回溯:

Traceback (most recent call last):
  File "test.py", line 19, in <module>
    response_body = urlopen(request).read()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 154, in urlopen
    return opener.open(url, data, timeout)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 437, in open
    response = meth(req, response)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 550, in http_response
    'http', request, response, code, msg, hdrs)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 475, in error
    return self._call_chain(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 409, in _call_chain
    result = func(*args)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/urllib2.py", line 558, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
>urllib2.HTTPError: HTTP Error 403: Forbidden

谢谢!

如果您使用的是 HTTP 基本身份验证,它看起来像是基于您的第二个代码示例,您希望在 [=] 中使用 Basic 授权类型而不是 OAuth 18=]。例如:

headers = {
  'Content-Type': 'application/json',
  'X-Socrata-Host': 'data.colorado.gov',
  'Content-Type': 'application/json',
  'Authorization': 'Basic {REDACTED}'
}

https://dev.socrata.com/docs/authentication.html

我已经修复了身份验证错误,并通过在 Python 3 上使用请求而不是使用 urllib2 来完成此操作。这是我更新的代码

#!/usr/bin/python
#coding: utf-8
import base64
import json
import requests

username = '{REDACTED}'
password = '{REDACTED}'
string = '%s:%s' % (username, password)
base64string = base64.b64encode(string.encode())
app_token = '{REDACTED}'

values = """
{
  "metadata" : {
    "id" : "TEST",
    "name" : "Business Entities_TEST"
  }
}
"""

headers = {
  'Accept': '*/*',  
  'X-Socrata-Host': 'data.colorado.gov',
  'Content-Type': 'application/json',
  'Authorization': 'Basic {REDACTED}',
  'X-App-Token': app_token
}

request = requests.get('https://data.colorado.gov/api/views/metadata/v1/4ykn-tg5h', data=values, headers=headers)
request.get_method = lambda: 'PATCH'

response_body = request.json()
print(response_body)