HttpResponse[Status=Found, StatusCode=302]" while integrating Salesforce to AzureDevops - salesforce

Integrating Salesforce to Azuredevops,
Creating Issue/Task/Bug to Azuredevops from the Salesforce custom page by using POST method RestApi.
Doing the same from postman service by using same url ,accesstoken etc and i am able to create task in the Azuredevops.
But issue is when i try the same from salesforce I am getting the response back as
"System.HttpResponse[Status=Found, StatusCode=302]" and <html><head><title>Object moved</title></head><body> <h2>Object moved to <a href="https://*******.*****.visualstudio.com/_signin*********************
I am using Namedcredential in the salesforce,
and written code like below,
Httprequest request = new HttpRequest();
Http http = new Http();
request.setMethod('POST');
request.setEndpoint('callout:Namedcred' + '/*****/******/_apis/wit/workitems/$issue?api-version=6.0' );
request.setHeader('Content-Type', 'application/json-patch+json');
request.setHeader('Authorization', '{!$Credential.Password}');
request.setBody(requestBody);
HttpResponse response = http.send(request);
I tried the same from the postman service and everything working good and also getting 200 OK status

Related

calling another web api end point from IdentityServer4 using IHttpClientFactory in .Net Core

I am using IdeneityServer4 and i have another Web Api controller in the identityserver4 api project and it is protected using identityserver 4 [Authorize(LocalApi.PolicyName)]. part of the authorization/authentication process, i need to call another web api (which does not exist in this identityserve4 api project) endpoint using IHttpClientFactory in .net core. i am getting 401 unauthorized error. if i call this different end point by passing a token it works but if i call it from the identityserver4 api it is not working. basically i am trying to do is
1. client (angular app) calls the identityserver4 api by passing credentials and get a token. (IT WoRKS)
2. client calls another end point (different web api controller) in identityserver4 api by passing token (IT WORKS).
in this end point i am trying to call another user notification api by passing the same token received and it is failing with 401. the notification api is also using the same identityserver4 for authorization.
3. if i just call the notification api separately by passing the token it works fine.
i do not see any examples calling another endpoint from identity server api. i see different web api controllers in the identity server 4 api
I am getting the token using following code in the web api controller method Identityserver 4 api and passing that token and required params to the SendUserInvitation method and its failing.
string bearerToken = null;
if (Request != null && Request.Headers.ContainsKey("Authorization"))
{
bearerToken = Request.Headers["Authorization"].FirstOrDefault().Replace("Bearer ", "");
}
======================================================================
public static async Task<bool> SendUserInvitation(string endpoint, string bearerToken, User userDetails, IHttpClientFactory _clientFactory)
{
var client = _clientFactory.CreateClient();
var request = new HttpRequestMessage(HttpMethod.Post, endpoint);
request.Headers.Add("Authorization", bearerToken);
var stringPayload = await Task.Run(() => JsonConvert.SerializeObject(userDetails));
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
request.Content = httpContent;
var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
var responseContent = await response.Content.ReadAsStringAsync();
return !string.IsNullOrEmpty(responseContent) ? true : false;
}
not sure why i am getting 401, the same token being passed to different endpoint but still getting 401.

Salesforce Callouts using Named Credentials

In Salesforce, Making a POST callout using apex and generating the bearer token.
Using the bearer token, making a GET callout and then parsing the JSON response.
However when I am trying to replicate the same procedure using Named Credential I fail to achieve the requirement.(O AUth 2.0)
HTTPRequest feedRequest = new HTTPRequest();
feedRequest.setEndpoint('callout:Salesforce_NC/the Get Callout Enpoint');
feedRequest.setHeader('Content-Type', 'application/json');
feedRequest.setMethod('GET');
HTTP http = new HTTP();
HTTPResponse feedResponse = http.send(feedRequest);
System.debug(feedResponse.getBody() + feedResponse.getStatusCode());

Slack & Salesforce - Rest API - Outgoing webhook

I am trying to connect to Salesforce Apex using POST. Slack has this pluggin, Outgoing Webhook which sends to a URL a POST request.
I have already created an APEX class to handle the request,
#RestResource(urlMapping='/test/accounts/*')
global with sharing class REST_slackAccount {
#HttpGet
global static void doGet() {
System.debug('Connected');
RestRequest req = RestContext.request;
RestResponse res = RestContext.response;
//accountId = req.requestURI.substring(req.requestURI.lastIndexOf('/')+1);
System.debug('Connected');
}
}
I am getting nothing from Salesforce when i trigger the request from Slack. What am i doing wrong.
I have already created a remote site settings.
The URL i used in Slack to make the POST request is:
https://test.salesforce.com/services//test/accounts/
Thank you.
It looks like that controller is only handling GET requests but Slack is doing a POST.

Call Parse.com REST services from Google App Engine

I have created a database in the Parse.com cloud. I now need to write a Servlet in my Google App Engine application to call the REST services on Parse. The REST services require
user authentication which is the Parse app id and the Javascript key.
...
URL url = new URL("https://api.parse.com/1/classes/OBJECT");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestProperty("Content-Type", "application/json");
Base64 enc = new Base64();
String userpassword = "{PARSE_APP_ID}" + ":" + "javascript-key={PARSE_JS_KEY}";
String encodedAuthorization = enc.encodeBase64String(userpassword.getBytes());
connection.setRequestProperty("Authorization", "Basic " + encodedAuthorization);
connection.setRequestMethod("GET");
...
I use org.apache.commons.codec.binary.Base64 for encoding to get REST call authenticated.
The Parse.com REST API recommends using the following request format to make a HTTP call:
https://myAppID:javascript-key=myJavaScriptKey#api.parse.com/1/classes/GameScore/Ed1nuqPvcm
The problem is that I kept getting
{"error":"unauthorized"}
Is there anyone having the experience with working with calling a authenticated REST service? Thanks!
EDIT.
URL url = new URL("https://api.parse.com/1/classes/OBJECT");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("X-Parse-Application-Id", "{APP_ID}");
connection.setRequestProperty("X-Parse-REST-API-Key", "REST_ID");
connection.setRequestProperty("Content-Type", "application/json");
I still got the same error while the responseCode was "200".
Cheers,
Use the header version of authentication instead:
connection.setRequestProperty("X-Parse-Application-Id", "app id here");
connection.setRequestProperty("X-Parse-REST-API-Key", "rest key here");

How to call a method in WSDL from Salesforce callout

I m a new bie to the salesforce, and I m trying to get the response by calling a wsdl file from the salesforce.
I dont know how to call a method of the WSDL from HTTP callouts
The code is something Like this :-
//HTTP request
HttpRequest req = new HttpRequest();
req.setEndpoint('domain.com/webservices/wwservice.php?wsdl');
req.setMethod('GET');
ANd I m calling the HTTP request and response from the TestMethod.
The result I m getting is System.HttpResponse[Status=null, StatusCode=0] .
Please help me out in this.
First you need to download the WSDL to your local machine, then upload the WSDL to Salesforce.com (Apex-->Generate from WSDL). This will generate the Apex classes needed to actually call web services described in the WSDL.
Anyway, it's all described here: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_callouts_wsdl2apex.htm

Resources