Socrata元数据API授权问题

时间:2018-01-08 21:11:59

标签: python python-2.7 authentication socrata soda

我正在尝试使用Socrata Metadata API通过python更新我的整个元数据资产库存。使用Metadata Api Docs中的python代码示例,我能够读取元数据,但由于错误403,即使使用正确的身份验证,我也无法更新它。我有管理员访问权限,访问秘密令牌和app令牌。我试图使用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减少花括号。代替授权'我也尝试了X-App-Token'无济于事。我觉得这是一个非常基本的修复,但我肯定错过了一些东西。

我还试图使用:

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

将变量base64string添加到我的标题中并遇到了同样的问题。基于:Update a Socrata dataset using app token and private in HTTP basic

如果有任何帮助,请点击我的追溯:

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

谢谢!

2 个答案:

答案 0 :(得分:0)

如果您正在使用HTTP基本身份验证,看起来您基于第二个代码示例,则需要在标头中使用Basic授权类型而不是OAuth。例如:

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

答案 1 :(得分:0)

我修复了身份验证错误,并通过在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)