2017-04-21 3 views
0

링크에서 Salesforce trailhead를 수행하고 있습니다 : https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_webservices.Apex에서 access_token을 처음 얻는 방법 외부에서 GET/POST 메서드를 호출하는 안심 서비스?

이 자습서에서는 GET 요청을 호출하기 위해 access_token을 사용했습니다. 그러나 그들은 외부에서 APEX Rest를 호출하는 중요한 단계 인access_token을 얻는 방법을 알려주지 않았습니다.

enter image description here

나는 그 나에게 오류를 말하는 아래와 같이 뭔가를 묶어 :

https://ap5.salesforce.com/services/oauth2/token?client_id="3MVG9d8..z.hDcPJZPIzGJ5UZDuKCOqbH8CCGCPnmwQuRbwLZ_2f.thbqWMX82H7JRGx4 
6VYyEkuwzQ9._ww5"&client_secret="1180508865211885204"&username="pXXXXXXXXXXXXXXX.com"&password="AgXXXXXXXX"&grant_type=password 

enter image description here

답변

0

다른 링크를 공유해 주셔서 감사합니다.

client_id, client_secret, username, password and grant_typeHTTP POST 헤더가 아닌 본문으로 보내야합니다.

HttpRequest req = new HttpRequest(); 
req.setMethod('POST'); 
req.setHeader('Content-Type','application/x-www-form-urlencoded'); 
req.setEndpoint('https://ap5.salesforce.com/services/oauth2/token'); 

String CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXX'; 
String CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXX'; 
String USERNAME = 'XXXXXXXXXXXXXX'; 
String PASSWORD = 'XXXXXXXXXXXXXX'; 

req.setBody('grant_type=password' + '&client_id='+CLIENT_ID + 
      '&client_secret='+CLIENT_SECRET + '&username='+USERNAME + '&password='+PASSWORD); 

Http http = new Http(); 
HTTPResponse response = http.send(req); 
System.debug('Body ' + response.getBody()); 
System.debug('Status ' + response.getStatus()); 
System.debug('Status code ' + response.getStatusCode()); 
0

당신은 액세스 토큰을 얻기 위해 API를 호출해야 할 수도 있습니다.

여기

비동기 공용 static 작업 GetAccessTokenByUserNamePasswordAuthenticationFlowAsync (문자열 이름, 문자열 암호 토큰 문자열, 문자열 consumerKey, 문자열 consumerSecret가) {

 HttpClient authClient = new HttpClient(); 


     string sfdcConsumerKey = consumerKey; 
     string sfdcConsumerSecret = consumerSecret; 

     string sfdcUserName = username; 
     string sfdcPassword = password; 
     string sfdcToken = token; 



     string loginPassword = sfdcPassword + sfdcToken; 

     HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string> 
      { 
       {"grant_type","password"}, 
       {"client_id",sfdcConsumerKey}, 
       {"client_secret",sfdcConsumerSecret}, 
       {"username",sfdcUserName}, 
       {"password",loginPassword} 
      } 
      ); 
     ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; //tuanv2t: Salesforce has changed to use TLS 1.1 -> 
     //tuanv2t: Without, responseString will like this {"error":"unknown_error","error_description":"retry your request"} 
     HttpResponseMessage message = await authClient.PostAsync("https://login.salesforce.com/services/oauth2/token", content); 

     string responseString = await message.Content.ReadAsStringAsync(); 

     //JObject obj = JObject.Parse(responseString); 
     //var oauthToken = (string)obj["access_token"]; 
     //var serviceUrl = (string)obj["instance_url"]; 

     var result = new GetAccessTokenResponse(); 
     result.HttpResponseMessage = message; 
     //Convert json string into object 
     var accessTokenAPI = JsonConvert.DeserializeObject<AccessTokenAPI>(responseString); 

     if (accessTokenAPI != null) 
     { 
      result.AccessToken = new AccessTokenModel(); 
      result.AccessToken.AccessToken = accessTokenAPI.access_token; 
      result.AccessToken.Id = accessTokenAPI.id; 
      result.AccessToken.InstanceUrl = accessTokenAPI.instance_url; 
      result.AccessToken.IssuedAt = accessTokenAPI.issued_at; 
      result.AccessToken.Signature = accessTokenAPI.signature; 
      result.AccessToken.TokenType = accessTokenAPI.token_type; 
     } 
     return result; 

    } 

그것은 수의 액세스 토큰을 얻기 위해 C#에서 내 코드입니다 내 모든 소스 코드 예제를 여기에서 다운로드하십시오 (SOAP API 포함) https://bitbucket.org/tuanv2t/salesforceapidemo

+0

정말 고마워요. 이것은 분명 도움이 될 것입니다. 우편 배달부 또는 고객 센터를 사용해야하는 경우에는 어떻게해야합니까? –

관련 문제