0

Okay so I'm using code very similar to this (https://gist.github.com/metadaddy-sfdc/1374762) to get authentication token and do simple query's using the libur2 for the rest api in python for a sales force database, but when I tried to follow the instructions which were given in this answer How to make HTTP DELETE method using urllib2?,

I cannot get it to work so that I can use delete, both codes use liburl but they seem to be in different format, so that I don't know how to apply the solution offered on stack exchange, to my code, as you can tell I am a beginner so any help would be greatly appreciated

edit: here is the code I'm using with keys/passwords blanked

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" 

I am looking to find out what I need to add to this to get DELETE working

Community
  • 1
  • 1
kujosHeist
  • 810
  • 1
  • 11
  • 25
  • 1
    It is difficult to give any advice without seeing code. – Jeremy J Starcher Jun 24 '13 at 15:55
  • It just "don't delete" -- or is there any error message? What is the [http status code](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html) of the server response? – Sylvain Leroux Jun 24 '13 at 16:04
  • Yeah the record doesn't delete, if you check the link to the other stack exchange answer, you have to do something in order to be able to use delete with urllib2, my code at the moment doesn't have this "something" as I am not sure how to implement it – kujosHeist Jun 24 '13 at 16:18

1 Answers1

5

Okay, found the solution to above for anyone with a similar problem:

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
HEADLESS_0NE
  • 3,416
  • 4
  • 32
  • 51
kujosHeist
  • 810
  • 1
  • 11
  • 25