I'm making an application in C# and I need to both search and post answers, so I need an access token.
I found some information, libraries, and code pieces:
- The authentication guide for the Stack Exchange API
- StacMan: a good library to use API in the .Net framework
I registered my application and I have a client ID. But, when I surf the following, I get a 500 error:
https://stackexchange.com/oauth/dialog?client_id=3408&scope=no_expiry&redirect_uri=https://stackexchange.com/oauth/login_success
The page says:
This app needs these permissions
•Perpetual access to this data
•To know what your accounts are on the Stack Exchange network
My c# code is:
string url2 = "https://stackexchange.com/oauth/dialog?client_id=3408&scope=no_expiry&redirect_uri=https://stackexchange.com/oauth/login_success";
HttpWebRequest requestLog = (HttpWebRequest)HttpWebRequest.Create(url2);
using (HttpWebResponse responseLog = (HttpWebResponse)requestLog.GetResponse())
{
NameValueCollection urlParameters = HttpUtility.ParseQueryString(responseLog.ResponseUri.Query);
// extract the access token from the url.
string accessToken = urlParameters["access_token"];
}
OR :
string url="https://stackexchange.com/oauth/dialog?client_id=3408&scope=no_expiry&redirect_uri=https://stackexchange.com/oauth/login_success";
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
StreamReader reader = new StreamReader(response.GetResponseStream());
string vals = reader.ReadToEnd();
foreach (string token in vals.Split('&'))
{
tokens.Add(token.Substring(0, token.IndexOf("=")),
token.Substring(token.IndexOf("=") + 1, token.Length - token.IndexOf("=") - 1));
}
}
string access_token = tokens["access_token"];
I searched in the net and couldn't find a good article about that. Please let me know what I need to make this work.