对于REST api,无法使用带有python的liburl2进行DELETE

kuj*_*ist 0 python rest salesforce urllib2

好的,所以我使用与此非常类似的代码(https://gist.github.com/metadaddy-sfdc/1374762)来获取身份验证令牌并使用libur2为python中的其余api执行简单查询以获取销售人员数据库,但是当我尝试按照本答案中给出的说明时如何使用urllib2制作HTTP DELETE方法?,

我无法让它工作,所以我可以使用删除,两个代码都使用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" 
Run Code Online (Sandbox Code Playgroud)

我想找出我需要添加到这个以使DELETE工作

kuj*_*ist 5

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

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
Run Code Online (Sandbox Code Playgroud)