2016-03-16 56 views
1

我正在研究簡單的Web應用程序,我需要從我的應用程序中的Outlook API獲取訪問令牌以使用員工姓名及其圖像。我編寫了代碼並並能夠通過Outlook登錄,但我的訪問令牌來爲null.Please在下面找到我的代碼:如何從Outlook API獲取訪問令牌到asp.net Web應用程序

public void GetResponseCallback(IAsyncResult asynchronousResult) 
{ 
    HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState; 

    // End the operation 
    HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult); 
    Stream streamResponse = response.GetResponseStream(); 
    StreamReader streamRead = new StreamReader(streamResponse); 
    string responseString = streamRead.ReadToEnd(); 
    access_token = responseString; 
    // Close the stream object 
    streamResponse.Close(); 
    streamRead.Close(); 

    // Release the HttpWebResponse 
    response.Close(); 
    allDone.Set(); 
} 
+0

您是否在您的請求中使用'UserAgent'? –

+0

不,我不使用UserAgent – user3714403

+0

你必須添加一些頭像'UserAgent'或'Content-Type'或'Connection',其中最重要的是響應第一個獲取請求的cookie。嘗試添加這些標題並嘗試相互更改這些標題以獲得所需的響應。 –

回答

1

HttpWebRequest的要求=(HttpWebRequest的)WebRequest.Create( 「某些URL」);

request.Method =「POST」;

request.ContentType =「application/x-www-form-urlencoded」;

request.UserAgent =「Mozilla/5.0(compatible; MSIE 9.0; Windows NT 7.1; Trident/5.0)」;

request.Accept =「/」;

request.UseDefaultCredentials = true;

request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;

doc.Save(request.GetRequestStream());

HttpWebResponse resp = request.GetResponse()as HttpWebResponse;

希望它有幫助

相關問題