I need get liste of SObjects using Oauth, with my code for this moment i can get the Token and i don't know how i can use it to get a liste of token///
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace BulkLoader
{
class Program
{
static string consumer_key = "my consumer key";
private static String consumer_secret = "my consumer secret";
private static String username = "my username";
private static String password = "my password + token";
public static TokenResponse oauth;
public static string GetToken(string client_id, string client_secret, string username, string password)
{
// Create the web request
HttpWebRequest request = GetRequest("https://login.salesforce.com/services/oauth2/token", "POST");
// Create POST data and convert it to a byte array.
string postData = string.Format("grant_type=password&client_id={0}&client_secret={1}&username={2}&password={3}&redirect_uri=resttest:callback", client_id, client_secret, username, password);
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Set the ContentType property of the WebRequest.
request.ContentType = "application/x-www-form-urlencoded";
// Set the ContentLength property of the WebRequest.
request.ContentLength = byteArray.Length;
// Get the request stream.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response
HttpWebResponse response = null;
string access_token = string.Empty;
try
{
response = (HttpWebResponse)request.GetResponse();
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
// Get the token
string jsonText = readStream.ReadToEnd();
readStream.Close();
JObject json = JObject.Parse(jsonText);
access_token = (string)json["access_token"];
}
catch (Exception ex)
{
throw new Exception("Unable to get token!", ex);
}
finally
{
if (response != null)
{
response.Close();
response = null;
}
request = null;
}
return access_token;
}
public static HttpWebRequest GetRequest(string url, string method, string userToken = "")
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
request.Method = method;
request.Accept = "application/json";
request.KeepAlive = true;
// Set Proxy
request.Proxy = new WebProxy() { Address = new Uri("http://proxyusers.intranet:8080"), UseDefaultCredentials = true };
// provide token
if (userToken.Trim().Length > 0)
request.Headers.Add("Authorization", "OAuth" + userToken);
return request;
}
/* I WANT GET LISTE OF SObjects */
public static String listSObjects(string oAuthToken)
{
String objs="";
HttpWebRequest request = GetRequest("https://natixis.my.salesforce.com/services/data/v26.0/sobjects/", "GET", oAuthToken);
return objs;
}
public static void Main()
{
String oAuthToken = GetToken(consumer_key, consumer_secret, username, password);
Console.WriteLine("oAuthToken: " + oAuthToken);
String objs = listSObjects(oAuthToken);
Console.WriteLine("-fin-");
Console.ReadKey();
}
}
}
Thank you.
답변 2개
Developer forum is the best place to get answers for these questions.
http://boards.developerforce.com/sforce/?category.id=developers