0

私はこのようなアクセストークンを取得しようとしています、

 string uri = "https://graph.facebook.com/oauth/access_token?client_id=" +
                       APP_ID +
                       "&redirect_uri=&client_secret=" + APP_SECRET + "&grant_type=client_credentials";
            var access_token = GetMyData(uri);
public async Task<string> GetMyData(string urlToCall)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlToCall);
            request.Method = "GET";
            HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync();
            using (var sr = new StreamReader(response.GetResponseStream()))
            {
                return sr.ReadToEnd();
            }
        }
        public static Task<HttpWebResponse> GetResponseAsync(this HttpWebRequest request)
        {
            var taskComplete = new TaskCompletionSource<HttpWebResponse>();
            request.BeginGetResponse(asyncResponse =>
            {
                try
                {
                    HttpWebRequest responseRequest = (HttpWebRequest)asyncResponse.AsyncState;
                    HttpWebResponse someResponse = (HttpWebResponse)responseRequest.EndGetResponse(asyncResponse);
                    taskComplete.TrySetResult(someResponse);
                }
                catch (WebException webExc)
                {
                    HttpWebResponse failedResponse = (HttpWebResponse)webExc.Response;
                    taskComplete.TrySetResult(failedResponse);
                }
            }, request);
            return taskComplete.Task;
        } 

var taskComplete = new TaskCompletionSource();でスタックします。時間がかかり、アクセストークンも取得できません。何が問題になっていますか?

4

1 に答える 1

1

あなたはおそらく次のコードでこれを達成することができます

public void GetMyData(string urlToCall)
{
    HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(urlToCall);
    request.BeginGetResponse(new AsyncCallback(ReadWebRequestCallback), request);
}

private static void ReadWebRequestCallback(IAsyncResult callbackResult)
{
    HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
    HttpWebResponse myResponse = (HttpWebResponse)myRequest.EndGetResponse(callbackResult);

    using (StreamReader httpwebStreamReader = new StreamReader(myResponse.GetResponseStream()))
    {
        // Here you get the result
        string results = httpwebStreamReader.ReadToEnd();               
    }
    myResponse.Close();
}
于 2013-03-22T07:24:58.347 に答える