示例#1
0
    /// <summary>
    /// Get a facebook access token
    /// </summary>
    /// <param name="domain">A reference to the current domain</param>
    /// <param name="code">The code to exchange for an acess token</param>
    /// <returns>The access token as a string</returns>
    public async static Task<string> GetFacebookAccessToken(Domain domain, string code)
    {
        // Create a string to get the access token
        string accessString = "";

        // Create the url
        string url = "https://graph.facebook.com/oauth/access_token?client_id=" + domain.facebook_app_id + "&redirect_uri="
            + HttpContext.Current.Server.UrlEncode(domain.web_address + "/user/facebook_login_callback") + "&client_secret=" 
            + domain.facebook_app_secret + "&code=" + code;

        // Create a http client
        HttpClient client = new HttpClient();
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        // Get the post
        HttpResponseMessage response = await client.GetAsync(url);

        // Make sure that the response is successful
        if (response.IsSuccessStatusCode)
        {
            accessString = await response.Content.ReadAsStringAsync();
        }

        // Dispose of the client
        client.Dispose();

        // Convert the string to a json object
        JObject token = AnnytabDataValidation.GetJsonObject(accessString);

        // Get the access token
        string access_token = token != null ? AnnytabDataValidation.GetJsonString(token["access_token"]) : "";

        // Return the access token
        return access_token;

    } // End of the GetFacebookAccessToken method