2013-03-21 37 views
0

我試圖得到這樣的的accessToken登錄,獲得了Facebook的的accessToken不使用的HttpWebRequest爲WP8

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 =新TaskCompletionSource();並且花費很多時間,並且也沒有獲得使用權限。什麼是錯的?

回答

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(); 
} 
實現這一目標
相關問題