In my c# console application I have a async
Task. I want to return a string
from it when I run the Task. something is wrong with the way I've written it.
Error Message:
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'.
Below is Code :
static void Main(string[] args)
{
string t1 = await DownloadToken(webserverLocal, tokenLocal);
}
static async Task<string> DownloadToken(string server, string token)
{
using (var client = new HttpClient())
{
var url = server + "token";
var parameters = new Dictionary<string, string> { { "username", usernameMain }, { "password", passwordMain }, { "grant_type", "password" } };
var encodedContent = new FormUrlEncodedContent(parameters);
var response = await client.PostAsync(url, encodedContent).ConfigureAwait(false);
if (response.StatusCode == HttpStatusCode.OK)
{
string content = await response.Content.ReadAsStringAsync();
JToken entireJson = JToken.Parse(content);
JProperty jName = entireJson.First.Value<JProperty>();
if (jName.Name == "access_token")
{
return jName.Value.ToString();
}
}
else
{
System.Windows.Forms.MessageBox.Show("ERROR: DownloadToken FAILED!!!", "ERROR");
Environment.Exit(0);
}
}
return null;
}
The other posts that are similar do not seem to return a string back to the main method.