WebException when trying to use Bing Translate API - c#

I'm attempting to translate some English text to Chinese using Bing Translate's API.
My code is essentially what was provided on MSDN, albeit with a few modifications.
using System;
using System.Text;
using System.Net;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Web;
using System.ServiceModel.Channels;
using System.ServiceModel;
namespace AutoTranslate2
{
public class Translator
{
private string authToken;
public Translator(string clientId, string clientSecret)
{
AdmAccessToken admToken;
AdmAuthentication admAuth = new AdmAuthentication("clientId", "client secret");
admToken = admAuth.GetAccessToken();
DateTime tokenReceived = DateTime.Now;
this.authToken = "Bearer " + admToken.access_token;
}
public string TranslateMethod(
string text,
string inputLang = "en",
string outputLang = "zh-CHS", // Chinese, simplified ('zh-CHT' is traditional Chinese)
string inputType = "text/html",
string outputType = "general")
{
// Add TranslatorService as a service reference, Address:http://api.microsofttranslator.com/V2/Soap.svc
TranslatorService.LanguageServiceClient client = new TranslatorService.LanguageServiceClient();
//Set Authorization header before sending the request
HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Method = "POST";
httpRequestProperty.Headers.Add("Authorization", this.authToken);
// Creates a block within which an OperationContext object is in scope.
string translationResult;
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
//Keep appId parameter blank as we are sending access token in authorization header.
translationResult = client.Translate("", "<p>" + text + "</p>", inputLang, outputLang, inputType, outputType);
}
return translationResult;
}
}
[DataContract]
public class AdmAccessToken
{
[DataMember]
public string access_token { get; set; }
[DataMember]
public string token_type { get; set; }
[DataMember]
public string expires_in { get; set; }
[DataMember]
public string scope { get; set; }
}
public class AdmAuthentication
{
public static readonly string DatamarketAccessUri = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13";
private string clientId;
private string clientSecret;
private string request;
public AdmAuthentication(string clientId, string clientSecret)
{
this.clientId = clientId;
this.clientSecret = clientSecret;
//If clientid or client secret has special characters, encode before sending request
this.request = string.Format("grant_type=client_credentials&client_id={0}&client_secret={1}&scope=http://api.microsofttranslator.com", HttpUtility.UrlEncode(clientId), HttpUtility.UrlEncode(clientSecret));
}
public AdmAccessToken GetAccessToken()
{
return HttpPost(DatamarketAccessUri, this.request);
}
private AdmAccessToken HttpPost(string DatamarketAccessUri, string requestDetails)
{
//Prepare OAuth request
WebRequest webRequest = WebRequest.Create(DatamarketAccessUri);
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(requestDetails);
webRequest.ContentLength = bytes.Length;
using (Stream outputStream = webRequest.GetRequestStream())
{
outputStream.Write(bytes, 0, bytes.Length);
}
WebResponse webResponse = webRequest.GetResponse();
using (webResponse)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(AdmAccessToken));
//Get deserialized object from JSON stream
AdmAccessToken token = (AdmAccessToken)serializer.ReadObject(webResponse.GetResponseStream());
return token;
}
}
}
}
...and somewhere else in my code, I do...
Translator t = new Translator(client_id, secret);
string output = t.TranslateMethod(text);
However, the code always returns an exception:
Unhandled Exception: System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at AutoTranslate2.AdmAuthentication.HttpPost(String DatamarketAccessUri, String requestDetails) in C:\Users\Deflect\AutoTranslate2\AutoTranslate2\Translate.cs:line 102
at AutoTranslate2.AdmAuthentication.GetAccessToken() in C:\Users\Deflect\AutoTranslate2\AutoTranslate2\Translate.cs:line 87
at AutoTranslate2.Translator..ctor(String clientId, String clientSecret) in C:\Users\Deflect\AutoTranslate2\AutoTranslate2\Translate.cs:line 26
at AutoTranslate2.Chinese.Parse(String rawText) in C:\Users\Deflect\AutoTranslate2\AutoTranslate2\Parse.cs:line 68
at AutoTranslate2.Program.Main(String[] args) in C:\Users\Deflect\AutoTranslate2\AutoTranslate2\Program.cs:line 19
I'll be the first to admit I really don't know what my code is doing, since I mostly copied and pasted from MSDN -- does anybody know why my code is returning an exception, and how I can get it to work?

It was this line:
AdmAuthentication admAuth = new AdmAuthentication("clientId", "client secret");
I forgot to swap the strings in the examples with the actual variables.

Related

C# Rest API error: "Missing key, signature and nonce parameters" for Bitstamp. I did include them in the code.. what could be wrong?

So the title pretty much says it all. I'm trying to make a POST request to this API: https://www.bitstamp.net/api/v2/balance/
I'm also adding all correct headers called X-Auth, as instructed here: https://www.bitstamp.net/api/
But when I then execute the code, it's telling me that I'm missing the key, signature and nonce parameters. When I debug, I do see them in the list. So I really don't understand why I am still getting this error. Can someone look inside the code and help me out, please?
Kind regards!
Here is the code:
using RestSharp;
using System;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp5
{
class Program
{
private readonly String _clientId = "xxx";
private readonly String _apiKey = "xxx";
private readonly String _apiSecret = "xxx";
static void Main()
{
Program program = new Program();
RestRequest request = new RestRequest("/api/v2/balance/", Method.POST);
program.AddApiAuthentication(request);
Console.ReadLine();
}
public void AddApiAuthentication(RestRequest restRequest)
{
var nonce = DateTime.Now.Ticks;
var signature = GetSignature(nonce, _apiKey, _apiSecret, _clientId);
long time = DateTime.UtcNow.Ticks / TimeSpan.TicksPerMillisecond;
string version = "v2";
string contentType = "application/x-www-form-urlencoded";
restRequest.AddParameter("X-Auth", _apiKey);
restRequest.AddParameter("X-Auth-Signature", signature);
restRequest.AddParameter("X-Auth-Nonce", nonce);
restRequest.AddParameter("X-Auth-Timestamp", time);
restRequest.AddParameter("X-Auth-Version", version);
restRequest.AddParameter("Content-Type", contentType);
RestClient client = new RestClient
{
BaseUrl = new Uri("https://www.bitstamp.net/")
};
IRestResponse response = client.Execute(restRequest);
Console.WriteLine(response.Content);
}
private string GetSignature(long nonce, string key, string secret, string clientId)
{
string msg = string.Format("{0}{1}{2}", nonce,
clientId,
key);
return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
}
public static byte[] SignHMACSHA256(String key, byte[] data)
{
HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
return hashMaker.ComputeHash(data);
}
public static byte[] StringToByteArray(string str)
{
return System.Text.Encoding.ASCII.GetBytes(str);
}
public static string ByteArrayToString(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
Based on what Lasse V. Karlsen told me, I could get it to work so that it no longer only displayed the API0000 error. I started getting other errors and with the help of a nice guy on the C# Discord channel I could work out a solution. I now have a completely working code here that will allow you to do private API calls using your API key.
Here is the code:
using RestSharp;
using System;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp5
{
class Program
{
private readonly string _apiKey = "BITSTAMP" + " " + "XXX";
private readonly string _apiSecret = "XXX";
private readonly string _URL = "www.bitstamp.net/api/v2/balance/";
private readonly string _queryParam = "";
private readonly string _contentType = "";
private readonly string _payloadString = "";
static void Main()
{
Program program = new Program();
RestRequest request = new RestRequest("api/v2/balance/", Method.POST);
program.AddApiAuthentication(request);
Console.ReadLine();
}
public void AddApiAuthentication(RestRequest restRequest)
{
var nonce = Guid.NewGuid();
var timer = (long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds);
Console.WriteLine(timer);
string version = "v2";
var signature = GetSignature(nonce, _apiKey, _apiSecret, timer);
restRequest.AddHeader("X-Auth", _apiKey);
restRequest.AddHeader("X-Auth-Signature", signature);
restRequest.AddHeader("X-Auth-Nonce", nonce.ToString()); ;
restRequest.AddHeader("X-Auth-Timestamp", timer.ToString()); ;
restRequest.AddHeader("X-Auth-Version", version);
RestClient client = new RestClient
{
BaseUrl = new Uri("https://www.bitstamp.net/")
};
IRestResponse response = client.Execute(restRequest);
Console.WriteLine(response.Content);
}
private string GetSignature(Guid nonce, string key, string secret, long timer)
{
string msg = $"{key}POST{_URL}{_queryParam}{_contentType}{nonce}{timer}v2{_payloadString}";
Console.WriteLine(msg);
return ByteArrayToString(SignHMACSHA256(secret, StringToByteArray(msg))).ToUpper();
}
public static byte[] SignHMACSHA256(String key, byte[] data)
{
HMACSHA256 hashMaker = new HMACSHA256(Encoding.ASCII.GetBytes(key));
return hashMaker.ComputeHash(data);
}
public static byte[] StringToByteArray(string str)
{
return System.Text.Encoding.ASCII.GetBytes(str);
}
public static string ByteArrayToString(byte[] hash)
{
return BitConverter.ToString(hash).Replace("-", "").ToLower();
}
}
}
Just change the XXX to your own API key and API secret, change the URL to whatever API you'd wish to access and you're good to go!
I hope it helps.
Nexigen.

im getting a error 401 unauthorized using the spotify api in c# wpf

Im getting this Error. System.Net.WebException: 'The remote server returned an error: (401) Unauthorized.' Code is provided below.
When i take out the method "PrintUsefulData(api)", everything seems to work fine
that method has a http client webrequest. Im trying to request the following https://api.spotify.com/v1/albums.
static void Main(string[] args)
{
_clientId = string.IsNullOrEmpty(_clientId)
? Environment.GetEnvironmentVariable("my_clientId")//my id
: _clientId;
_secretId = string.IsNullOrEmpty(_secretId)
? Environment.GetEnvironmentVariable("my_secretId") // my id
: _secretId;
AuthorizationCodeAuth auth =
new AuthorizationCodeAuth(_clientId, _secretId, "http://localhost:5002", "http://localhost:5002", Scope.PlaylistReadPrivate | Scope.PlaylistReadCollaborative);
auth.AuthReceived += AuthOnAuthReceived;
auth.Start();
auth.OpenBrowser();
Console.ReadLine();
auth.Stop(0);
}
private static async void AuthOnAuthReceived(object sender,
AuthorizationCode payload)
{
AuthorizationCodeAuth auth = (AuthorizationCodeAuth)sender;
auth.Stop();
Token token = await auth.ExchangeCode(payload.Code);
SpotifyWebAPI api = new SpotifyWebAPI
{
AccessToken = token.AccessToken,
TokenType = token.TokenType
};
PrintUsefulData(api);
}
private static async void PrintUsefulData(SpotifyWebAPI api)
{
RestClient rClient = new RestClient();
rClient.endPoint = "https://api.spotify.com/v1/albums";
string strResponse = string.Empty;
strResponse = rClient.getRequest();
}
}
}
public enum HttpVerb
{
GET,
POST,
PUT,
DELETE
}
class RestClient
{
public string endPoint { get; set; }
public HttpVerb httpMethod { get; set; }
public RestClient()
{
endPoint = string.Empty;
httpMethod = HttpVerb.GET;
}
public string getRequest()
{
string strResponseVal = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = httpMethod.ToString();
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new ApplicationException("error code: " + response.StatusCode);
}
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
strResponseVal = reader.ReadToEnd();
}
}
}
}
return strResponseVal;
}
}
}
There could be a couple of things going on here but here's my shot at helping based on what code you've posted:
Token Expiration - If you get a 401 error on a request then you need to use the Refresh Token which should have been supplied at the point of authorisation to get a new Access Token. This should apply to ANY call you make to the API.
Request Parameters - The endpoint you're calling (https://api.spotify.com/v1/albums) requires the parameter ids so the Spotify API knows what 'albums' you would like to return.
More info: https://developer.spotify.com/documentation/web-api/reference/albums/get-several-albums/
Another thing to check:
- Scope - make sure when you Auth to the API you are setting the required scope you need to perform actions in the future. I don't think this applies specifically in this case but worth noting.

Error 400 - Bad Request an Creating user with Microsoft Graph

I am trying to create a new user in my tenant using Microsoft Graph (v1.0) with help of the Microsoft doc.
When I create my user, I always get an error 400 bad request as response.
I am using HttpClient to make the post Request.
My Function :
private async Task<string> BuildUser(string token, string query)
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
UserCreation uc = new UserCreation
{
accountEnabled = this.checkBoxActive.Checked,
displayName = this.textBoxDN.Text,
mailNickName = this.textBoxMail.Text,
passwordProfile = new PasswordProfile { forceChangePasswordNextSignIn = this.checkBoxChangeMDP.Checked, password = this.textBoxPassword.Text},
userPrincipalName = this.textBoxUPN.Text
};
string json = JsonConvert.SerializeObject(uc);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync(query, content).Result;
return response.ToString();
}
My token is valid and i am able to make simple Get requests, my app have the authorizations mentioned here.
For example, my json var can contains :
{
"accountEnabled":true,
"displayName":"cyril testgraphh",
"mailNickName":"cyriltestgraphh",
"userPrincipalName":"cyriltestgraphh#mytenant.fr",
"passwordProfile":{
"forceChangePasswordNextSignIn":true,
"password":"XXX"
}
}
EDIT :
I solved my problem by using Microsoft Graph objects (Microsoft.Graph.User and Microsoft.Graph.PasswordProfile) and add .onmicrosoft.com to my upn
You should check your code. I tried the following code, it works well.
using Microsoft.Graph;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp4
{
class Program
{
static void Main(string[] args)
{
HttpClient httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
UserCreation uc = new UserCreation
{
accountEnabled = true,
displayName = "cyril testgraphh",
mailNickName = "cyriltestgraphh",
passwordProfile = new PasswordProfile { ForceChangePasswordNextSignIn = false, Password = "Password!" },
userPrincipalName = "XXXXXX#jmaster.onmicrosoft.com"
};
string json = JsonConvert.SerializeObject(uc);
var content = new StringContent(json, Encoding.UTF8, "application/json");
HttpResponseMessage response = httpClient.PostAsync("https://graph.microsoft.com/v1.0/users", content).Result;
Console.Write(response);
Console.ReadLine();
}
}
class UserCreation
{
public bool accountEnabled { get; internal set; }
public string displayName { get; internal set; }
public string mailNickName { get; internal set; }
public string userPrincipalName { get; internal set; }
public PasswordProfile passwordProfile { get; internal set; }
}
}
And the response like this:
In my case the only thing returned from Azure Graph API was the error 400 - Bad Request... nothing else. :(
What I did to solve it? I was using the full blown Group object from Microsoft.Graph NuGet package to create a Group on Azure B2C. However, I was just using 5 properties of that object. While serializing to JSON it was serializing all properties of that class and for some reason the Graph API was barking about a malformed request.
So I just created an anonymous type with the properties that I needed:
var group = new
{
DisplayName = theGroupName,
Description = $"User group for {theGroupName}",
MailNickname = theGroupName.Replace(" ", string.Empty),
MailEnabled = false,
SecurityEnabled = true
};
After sending this streamlined object to the Graph API endpoint, I got a success response.

Patching to TFS API with JSON gives 400 error (Bad Request)

I'm trying to do a patch http request to change one of the fields in TFS through the TFS REST API. I've tried several approaches, but I always end up with 400 error. Here is what I have right now:
public void SetFieldValue(string value, string path, int id)
{
var httpWebRequest = (HttpWebRequest)WebRequest.Create(PatchwebAPIUrl("wit/workitems", id.ToString()));
httpWebRequest.ContentType = "application/json-patch+json";
httpWebRequest.Method = "PATCH";
httpWebRequest.Headers["Authorization"] = "Basic" + Base64authorizationToken();
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = "[{\"op\":\"replace\"," +
$"\"path\":\"{path}\"," +
$"\"value\":\"{value}\"}}]";
streamWriter.Write(JsonConvert.SerializeObject(json));
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var result = streamReader.ReadToEnd();
}
}
And the test method that calls this method:
[TestMethod()]
public void setFieldValue()
{
TFSWebAPIImplementation webAPI = new TFSWebAPIImplementation();
webAPI.SetFieldValue("654321", "/fields/Custom.Tracking", 61949);
}
The PatchwebAPIUrl("...") Method is fine, and returns a good URL, when I navigate to it I get the JSON data that I want to edit. I'm not 100% on the path variable but it's used the same as the example provided from Microsoft. The authorization works, just based on the fact that when I mess with it I get a 401 instead.
This is my sample code:
Class for work item:
public class WorkItemAtrr
{
[JsonProperty("id")]
public int id;
[JsonProperty("rev")]
public int rev;
[JsonProperty("fields")]
public Dictionary<string, string> fields;
[JsonProperty("_links")]
public Dictionary<string, Link> _links;
[JsonProperty("relations")]
public List<Relation> relations;
[JsonProperty("url")]
public string url;
}
public class Link
{
[JsonProperty("href")]
public string href;
}
public class Relation
{
[JsonProperty("rel")]
public string rel;
[JsonProperty("url")]
public string url;
[JsonProperty("attributes")]
public RelationAttribute attributes;
}
public class RelationAttribute
{
[JsonProperty("comment")]
public string comment = "";
[JsonProperty("isLocked")]
public bool isLocked;
}
Class for new and updated fields:
public class NewField
{
[JsonProperty("op")]
public string op = "add";
[JsonProperty("path")]
public string path;
[JsonProperty("value")]
public object value;
}
Class for exceptions:
public class RestApiExceptionContainer
{
[JsonProperty("id")]
public int id;
[JsonProperty("innerException")]
public string innerException;
[JsonProperty("message")]
public string message;
[JsonProperty("typeName")]
public string typeName;
[JsonProperty("typeKey")]
public string typeKey;
[JsonProperty("errorCode")]
public int errorCode;
[JsonProperty("evenId")]
public int eventId;
}
Method for update a work item:
private static WorkItemAtrr UpdateWorkItemRest()
{
Dictionary<string, string> _fields = new Dictionary<string, string>();
_fields.Add("REFERENCE_NAME", "VALUE");
var _updatedWi = UpdateWorkItem("ID", _fields).Result;
}
Method for preparing request:
public async Task<WorkItemAtrr> UpdatedWorkItem(int pId, Dictionary<String, String> pFields)
{
//PATCH https://{instance}/DefaultCollection/_apis/wit/workitems/{id}?api-version={version}
string _query_url = String.Format("https://YOUR_SERVER/DefaultCollection/_apis/wit/workitems/{id}?api-version=1.0", pId);
List<Object> flds = new List<Object>();
foreach (var _key in pFields.Keys)
flds.Add(new NewField { op = "add", path = "/fields/" + _key, value = pFields[_key] });
HttpResponseMessage _response = await DoRequest(_query_url, JsonConvert.SerializeObject(flds), ClientMethod.PATCH);
return JsonConvert.DeserializeObject<WorkItemAtrr>(await ProcessResponse(_response));
}
Universal method for request:
private async Task<HttpResponseMessage> DoRequest(string pRequest, string pBody, ClientMethod pClientMethod)
{
try
{
HttpClientHandler _httpclienthndlr = new HttpClientHandler();
//update for your auth
if (UseDefaultCredentials) _httpclienthndlr.Credentials = CredentialCache.DefaultCredentials;
else if (TFSDomain == "") _httpclienthndlr.Credentials = new NetworkCredential(TFSUserName, TFSPassword);
else _httpclienthndlr.Credentials = new NetworkCredential(TFSUserName, TFSPassword, TFSDomain);
using (HttpClient _httpClient = new HttpClient(_httpclienthndlr))
{
switch (pClientMethod)
{
case ClientMethod.GET:
return await _httpClient.GetAsync(pRequest);
case ClientMethod.POST:
return await _httpClient.PostAsync(pRequest, new StringContent(pBody, Encoding.UTF8, "application/json"));
case ClientMethod.PATCH:
var _request = new HttpRequestMessage(new HttpMethod("PATCH"), pRequest);
_request.Content = new StringContent(pBody, Encoding.UTF8, "application/json-patch+json");
return await _httpClient.SendAsync(_request);
default:
return null;
}
}
}
catch (Exception _ex)
{
throw new Exception("Http Request Error", _ex);
}
}
Universal method for response:
public async Task<string> ProcessResponse(HttpResponseMessage pResponse)
{
string _responseStr = "";
if (pResponse != null)
{
if (pResponse.IsSuccessStatusCode)
_responseStr = await pResponse.Content.ReadAsStringAsync();
else
{
_responseStr = await pResponse.Content.ReadAsStringAsync();
var _error = JsonConvert.DeserializeObject<RestApiExceptionContainer>(_responseStr);
throw new RestApiException(_error);
}
}
return _responseStr;
}
A 400 means that the request was malformed. In other words, the data stream sent by the client to the server didn't follow the rules.
In the case of a REST API with a JSON payload, 400's are typically, used to indicate that the JSON is invalid in some way according to the API specification for the service.
So, the issue is caused by the JSON body.
Just try it like below:
string json = "[{\"op\":\"replace\",\"path\":\"/fields/System.Title\",\"value\":\"Title\"}]";
You can also use below sample to update the fields with PATCH method via the REST API, it works for me:
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using Newtonsoft.Json;
namespace UpdateWorkItemFiled0411
{
class Program
{
static void Main(string[] args)
{
string password = "xxxx";
string credentials = Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(string.Format("{0}:{1}", "username", password )));
Object[] patchDocument = new Object[1];
patchDocument[0] = new { op = "replace", path = "/relations/attributes/comment", value = "Adding traceability to dependencies" };
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", credentials);
var patchValue = new StringContent(JsonConvert.SerializeObject(patchDocument), Encoding.UTF8, "application/json-patch+json");
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, "http://server:8080/tfs/DefaultCollection/_apis/wit/workitems/21?api-version=1.0") { Content = patchValue };
var response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}
}
}
}
}
Also you may use nugate package Microsoft.TeamFoundationServer.Client.
Connect to team project from here:
using Microsoft.TeamFoundation.Core.WebApi;
using Microsoft.VisualStudio.Services.Common;
...
//create uri and VssBasicCredential variables
Uri uri = new Uri(url);
VssBasicCredential credentials = new VssBasicCredential("", personalAccessToken);
using (ProjectHttpClient projectHttpClient = new ProjectHttpClient(uri, credentials))
{
IEnumerable<TeamProjectReference> projects = projectHttpClient.GetProjects().Result;
}
my code to add comments:
JsonPatchDocument PatchDocument = new JsonPatchDocument();
PatchDocument.Add(
new JsonPatchOperation()
{
Operation = Operation.Add,
Path = "/fields/System.History",
Value = "Changes from script"
}
);
VssCredentials Cred = new VssCredentials(true);
WorkItemTrackingHttpClient WIClient = new WorkItemTrackingHttpClient(new Uri("http://YOUR_SERVER/tfs/DefaultCollection"), Cred);
WorkItem result = WIClient.UpdateWorkItemAsync(PatchDocument, id).Result;
Okay guys, so unfortunately none of your solutions worked, and I think it's because there was always an extra set of curly brackets on the outside that the TFS API didn't like. Here is my solution that solved my problem:
public void SetFieldValue(string value, string path, int id)
{
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Base64authorizationToken());
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
writer.WriteStartArray(); // [
writer.WriteStartObject(); // {
writer.WritePropertyName("op"); // "Product:"
writer.WriteValue("replace");
writer.WritePropertyName("path");
writer.WriteValue(path);
writer.WritePropertyName("value");
writer.WriteValue(value);
writer.WriteEndObject(); //}
writer.WriteEnd(); // ]
}
var method = new HttpMethod("PATCH");
var request = new HttpRequestMessage(method, PatchwebAPIUrl("wit/workitems", id.ToString())) { Content = new StringContent(sb.ToString().Trim(new char[] {'{','}'}), Encoding.UTF8, "application/json-patch+json") };
var response = client.SendAsync(request).Result;
if (response.IsSuccessStatusCode)
{
var result = response.Content.ReadAsStringAsync().Result;
}
}
}

reading my json response from a post request i sent

I made a post request class that I could re-use to make POST requests to an external api and return the objects they send me (JSON):
class PostRequest
{
private Action<DataUpdateState> Callback;
public PostRequest(string urlPath, string data, Action<DataUpdateState> callback)
{
Callback = callback;
// form the URI
UriBuilder fullUri = new UriBuilder(urlPath);
fullUri.Query = data;
// initialize a new WebRequest
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(fullUri.Uri);
request.Method = "POST";
// set up the state object for the async request
DataUpdateState dataState = new DataUpdateState();
dataState.AsyncRequest = request;
// start the asynchronous request
request.BeginGetResponse(new AsyncCallback(HandleResponse),
dataState);
}
private void HandleResponse(IAsyncResult asyncResult)
{
// get the state information
DataUpdateState dataState = (DataUpdateState)asyncResult.AsyncState;
HttpWebRequest dataRequest = (HttpWebRequest)dataState.AsyncRequest;
// end the async request
dataState.AsyncResponse = (HttpWebResponse)dataRequest.EndGetResponse(asyncResult);
if (dataState.AsyncResponse.StatusCode.ToString() == "OK")
{
Callback(dataState); // THIS IS THE LINE YOU SHOULD LOOK AT :)
}
}
}
public class DataUpdateState
{
public HttpWebRequest AsyncRequest { get; set; }
public HttpWebResponse AsyncResponse { get; set; }
}
}
the Callback method gets the datastate object and pushes it to this function:
public void LoadDashboard( DataUpdateState dataResponse )
{
Stream response = dataResponse.AsyncResponse.GetResponseStream();
//Encoding encode = System.Text.Encoding.GetEncoding("utf-8");
//StreamReader readStream = new StreamReader(response, encode);
//readStream.Close();
Deployment.Current.Dispatcher.BeginInvoke(() => {
App.RootFrame.Navigate(new Uri("/Interface.xaml", UriKind.RelativeOrAbsolute));
});
}
I'm now unsure of how to get the body of that reply that has been sent to me from the API. It is returning a json format, I need to be able to map it to a nice C# class and use it to display stuff on the phone.
I can't find an example that doesn't use JSON.NET (which doesn't have an assembly for windows phone 8)
This is error I get installing HTTPClient class:
Attempting to resolve dependency 'Microsoft.Bcl (≥ 1.1.3)'.
Attempting to resolve dependency 'Microsoft.Bcl.Build (≥ 1.0.4)'.
Successfully installed 'Microsoft.Bcl.Build 1.0.10'.
Successfully installed 'Microsoft.Bcl 1.1.3'.
Successfully installed 'Microsoft.Net.Http 2.2.13'.
Successfully added 'Microsoft.Bcl.Build 1.0.10' to UnofficialPodio.
Executing script file ***\packages\Microsoft.Bcl.Build.1.0.10\tools\Install.ps1'.
This reference cannot be removed from the project because it is always referenced by the compiler.
This reference cannot be removed from the project because it is always referenced by the compiler.
This reference cannot be removed from the project because it is always referenced by the compiler.
This reference cannot be removed from the project because it is always referenced by the compiler.
Executing script file ***\packages\Microsoft.Bcl.Build.1.0.10\tools\Uninstall.ps1'.
Successfully uninstalled 'Microsoft.Bcl 1.1.3'.
Successfully uninstalled 'Microsoft.Bcl.Build 1.0.10'.
Install failed. Rolling back...
Failed to add reference to 'System.IO'.
"{
\"access_token\": \"123803120312912j\",
\"token_type\": \"bearer\",
\"ref\": {
\"type\": \"user\",
\"id\": 123123
},
\"expires_in\": 28800,
\"refresh_token\": \"234234f23f423q432f\"
}"
...
public class Auth
{
[DataMember(Name = "access_token")]
public string AccessToken { get; set; }
[DataMember(Name = "token_type")]
public string TokenType { get; set; }
[DataMember(Name = "expires_in")]
public string ExpiresIn { get; set; }
[DataMember(Name = "refresh_token")]
public string RefreshToken { get; set; }
//[DataMember(Name = "ref")]
//public string Ref { get; set; }
}
To get the response data you need to call GetResponseStream() on the HttpWebResponse object and then read from the stream. Something like this:
using (Stream s = response.GetResponseStream())
{
using (TextReader textReader = new StreamReader(s, true))
{
jsonString = textReader.ReadToEnd();
}
}
To get the data from the json string, you need to create a data contract class to describe the json data exactly like this:
[DataContract]
public class ApiData
{
[DataMember(Name = "name")] <--this name must be the exact name of the json key
public string Name { get; set; }
[DataMember(Name = "description")]
public string Description { get; set; }
}
Next you can deserialize the json object from the string:
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(jsonString)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(ApiData));
ApiData obj = (ApiData)serializer.ReadObject(stream);
return obj;
}
WebRequest will work fine, but I would recommend installing the NuGet package for the HttpClient class. It makes life much simpler. for instance, you could make the above request code in just a few lines:
HttpClient httpClient = new HttpClient();
HttpRequestMessage msg = new HttpRequestMessage(new HttpMethod("POST"), escapedUrl);
HttpResponseMessage response = await httpClient.SendAsync(msg);
In answer to you question below, here is the generic json converter code that I use:
public static class JsonHelper
{
public static T Deserialize<T>(string json)
{
using (MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
T obj = (T)serializer.ReadObject(stream);
return obj;
}
}
public static string Serialize(object objectToSerialize)
{
using (MemoryStream ms = new MemoryStream())
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(objectToSerialize.GetType());
serializer.WriteObject(ms, objectToSerialize);
ms.Position = 0;
using (StreamReader sr = new StreamReader(ms))
{
return sr.ReadToEnd();
}
}
}
}
JSON.Net does support windows phone 8, it's available as a portable class library according to this answer.
Just try adding the package via nuget...

Categories