I'm using DevDefined.Oauth to get my Oauth header. It works perfectly fine for my GET request, but when I use it for POST requests it returns oauth_problem=oauth_signature invalid.
private static String GetDevDefinedOauthHeader(String consumerKey, String consumerSecret, IToken accessToken, Uri requestUri, AsymmetricAlgorithm jKey, String restMethod)
{
OAuthConsumerContext consumerContext = new OAuthConsumerContext
{
ConsumerKey = consumerKey,
SignatureMethod = SignatureMethod.RsaSha1,
ConsumerSecret = consumerSecret,
UseHeaderForOAuthParameters = true,
Key = jKey
};
OAuthSession oSession = new OAuthSession(consumerContext)
{
AccessToken = new TokenBase
{
Token = accessToken.Token,
ConsumerKey = accessToken.ConsumerKey,
TokenSecret = accessToken.TokenSecret,
SessionHandle = accessToken.SessionHandle
}
};
IConsumerRequest consumerRequest = oSession.Request();
consumerRequest = ConsumerRequestExtensions.ForMethod(consumerRequest, restMethod);
consumerRequest = ConsumerRequestExtensions.ForUri(consumerRequest, requestUri);
consumerRequest = consumerRequest.SignWithToken();
return consumerRequest.Context.GenerateOAuthParametersForHeader();
}
This is my GET request:
String oauthHeader = GetDevDefinedOauthHeader(consumerKey, consumerSecret, sessionAccessToken, new Uri(baseUrl + "/rest/api/latest/issue/" + issueKey), key, "GET");
String newHeader = header.Split(' ')[1];
hClient.DefaultRequestHeaders.Accept.Clear();
hClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
hClient.SetAuthorization("OAuth", newHeader);
HttpResponseMessage response = await hClient.GetAsync("/rest/api/latest/issue/HW-1");
This is my POST request:
String oauthHeader = GetDevDefinedOauthHeader(consumerKey, consumerSecret, sessionAccessToken, new Uri(baseUrl + "/rest/api/latest/issue/" + issueKey), key, "POST");
String newHeader = header.Split(' ')[1];
hClient.DefaultRequestHeaders.Accept.Clear();
hClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
hClient.SetAuthorization("OAuth", newHeader);
HttpResponseMessage response = await hClient.PostAsJsonAsync("/rest/api/latest/version", versionObject);
UPADTE:
realized my issue was the url I was passing to the GetDevDefinedOauthHeader() method. It wasn't getting the right URL passed to it. So instead of baseUrl, I passed the baseUrl + endpointUrl and it worked like a charm.
Hi,
Could please post here an example showing how the jKey is generated ?
Thank you.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.