- Home /
This question was
closed Oct 18, 2016 at 11:43 AM by
RangerDog for the following reason:
Other
Question by
RangerDog · Oct 18, 2016 at 07:53 AM ·
apiwebconnection
Multiple Errors while connecting to Webapi, No errors in VS Studio
Hai! So i got this code from someone from websites, I edited it so that it all works fine. In Visual Studio it doesn't give an error, (errors down the code), Code: (Censored website and such)
using UnityEngine;
using System;
using System.Collections.Generic;
using System.Net;
using Newtonsoft.Json;
using System.Threading.Tasks;
using System.Net.Http;
public class GetOnlineContainers : MonoBehaviour
{
protected string username = "**************";
protected string password = "**************"";
protected string connection = "**************"";
protected string accessToken = "**************"";
public GetOnlineContainers()
{
HttpWebRequest req = WebRequest.Create("**************"")
as HttpWebRequest;
var client = new System.Net.Http.HttpClient { BaseAddress = new Uri("**************"") };
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
var result = Post("/api/public/token", GetTokenFormContent(username, password, connection), false).Result;
Debug.Log(JsonConvert.DeserializeObject<TokenResult>(result));
}
private static System.Net.Http.FormUrlEncodedContent GetTokenFormContent(string username, string password, string connection)
{
return new System.Net.Http.FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
new KeyValuePair<string, string>("connection", connection)
});
}
private async Task<string> Post(string url, HttpContent content, bool requireAuthorization)
{
using (var client = new System.Net.Http.HttpClient { BaseAddress = new Uri(string.Empty) }) //GetBaseUrl()) })
{
if (requireAuthorization)
{
//var accessToken = GetAccessToken();
//if (!string.IsNullOrEmpty(accessToken))
//{
// client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", accessToken);
//}
}
var response = await client.PostAsync(new Uri(url, UriKind.RelativeOrAbsolute), content);
return await response.Content.ReadAsStringAsync();
}
}
public class TokenResult
{
[JsonProperty("access_token")]
public string AccessToken { get; set; }
[JsonProperty("token_type")]
public string TokenType { get; set; }
[JsonProperty("expires_in")]
public int ExpiresIn { get; set; }
[JsonProperty("userName")]
public string UserName { get; set; }
[JsonProperty("error")]
public string Error { get; set; }
[JsonProperty("error_description")]
public string ErrorDescription { get; set; }
}
}
Errors:
Assets/GetOnlineContainers.cs(40,24): error CS0081: Type parameter declaration must be an identifier not a type
Assets/GetOnlineContainers.cs(40,35): error CS1519: Unexpected symbol `Post' in class, struct, or interface member declaration
Assets/GetOnlineContainers.cs(40,32): error CS1520: Class, struct, or interface method must have a return type
Assets/GetOnlineContainers.cs(52,39): error CS1525: Unexpected symbol `client'
Assets/GetOnlineContainers.cs(53,33): error CS1525: Unexpected symbol `response'
The first 2, to be honest i have no idea what could be wrong with it. The third one, there is a return on the using, but that's on the wrong level. The 4th and 5th, not sure what could be wrong there, considering its the correct syntax.
If anyone could explain what's going wrong here, would be awesome!
Comment