【问题标题】:Cannot get DELETE working with liburl2 with python for REST api无法使用用于 REST api 的 python 与 liburl2 一起使用 DELETE
【发布时间】:2013-06-24 15:36:38
【问题描述】:

好的,我正在使用与此非常相似的代码 (https://gist.github.com/metadaddy-sfdc/1374762) 获取身份验证令牌并使用 libur2 为销售人员数据库在 python 中的其余 api 执行简单查询,但是当我尝试按照此答案How to make HTTP DELETE method using urllib2? 中给出的说明进行操作时,

我无法让它工作,所以我可以使用删除,两个代码都使用 liburl 但它们似乎采用不同的格式,所以我不知道如何将堆栈交换提供的解决方案应用于我的代码,如您所知,我是初学者,因此将不胜感激任何帮助

编辑: 这是我正在使用的代码,其中键/密码为空白

import urllib
import urllib2
import json
import pprint
import re
import subprocess

def authorise():
    consumer_key = '**********************'
    consumer_secret = '**************'
    username = '***********'
    password = '*****************'
    login_server = 'https://login.salesforce.com'

    token_url = login_server+'/services/oauth2/token'

    params = urllib.urlencode({
      'grant_type': 'password',
      'client_id': consumer_key,
      'client_secret': consumer_secret,
      'username': username,
      'password': password
    })
    data = urllib2.urlopen(token_url, params).read()
    oauth = json.loads(data)
    return oauth

def country_id_query(params):
    query_url = oauth['instance_url']+'/services/data/v23.0/query?%s' % params 
    headers = {
      'Authorization': 'OAuth '+oauth['access_token']
    }
    req = urllib2.Request(query_url, None, headers)
    data = urllib2.urlopen(req).read()
    result = json.loads(data)
    id = result['records'][0]['Id']
    return id

oauth = authorise()
token = oauth['access_token']
print "\ntoken is = " + token

params = urllib.urlencode({
  'q': 'SELECT id from Country__c WHERE name = \'A New Found Land\''

})
id = country_id_query(params)
print "\nCountry id is "+id + "\n" 

我正在寻找我需要添加什么才能让 DELETE 工作

【问题讨论】:

  • 不看代码很难给出建议。
  • 它只是“不要删除”——还是有任何错误信息?服务器响应的http status code 是什么?
  • 是的,记录没有删除,如果你检查到另一个堆栈交换答案的链接,你必须做一些事情才能使用 urllib2 删除,我的代码现在没有没有这个“东西”,因为我不知道如何实现它

标签: python rest salesforce urllib2


【解决方案1】:

好的,为遇到类似问题的人找到了上述解决方案:

def delete_country(id):
    query_url = oauth['instance_url']+'/services/data/v23.0/sobjects/Country__c/%s' % id + '/'
    headers = {
      'Authorization': 'OAuth '+oauth['access_token']
    }
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    req = urllib2.Request(query_url, None, headers)
    req.get_method = lambda: 'DELETE'  # creates the delete method
    url = urllib2.urlopen(req)  # deletes database item

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2012-02-21
    • 1970-01-01
    • 1970-01-01
    • 2014-02-26
    相关资源
    最近更新 更多