首页 文章

WebClient在物理设备和模拟器上失败

提问于
浏览
0

更新:也许只是我不理解oAuth如何运作?我尝试在http://www.apikitchen.com上手动运行查询,我也得到400错误!只是为了确定,我在这里正确构建了URL吗? POST URL:https://api.bufferapp.com/1/oauth2/token.json?client_id = [hidden]&client_secret = [hidden]&redirect_uri = http://apps.joel-murphy.com/buffer/code/success .php&code = [我从缓冲区获得的访问代码以1 /]和grant_type = authorization_code开头

原帖:

我正在构建一个Windows Phone应用程序,它需要使用来自网站的数据 . 该网站使用oAuth对用户进行身份验证 .

我使用内置的Web浏览器控件来发出GET请求来验证用户身份 . 官方文档要求URL结构如下:

GET https://bufferapp.com/oauth2/authorize? client_id = ...&redirect_uri = ...&response_type = code

这部分我的应用程序工作 . 虽然在从服务器交换访问令牌的授权令牌时,我遇到了问题 . 官方文档要求URL结构如下:

POST https://api.bufferapp.com/1/oauth2/token.json? client_id = ...&client_secret = ...&redirect_uri = ...&code = ...&grant_type = authorization_code

如果我错了,请纠正我,但据我所知,除非提交表格,否则无法从浏览器发出POST请求 . 出于这个原因,我决定使用 WebClient 类将数据提交给服务器 . 但是,无论我是在实际设备上还是在Visual Studio模拟器上运行我的代码,我总是会收到以下错误:

远程服务器返回错误:NotFound

enter image description here

有没有人知道下面的代码有什么问题?我在两天内花了5个多小时试图解决这个错误,但似乎没有任何工作 .

我正在使用的代码:

WebClient wc = new WebClient();
    wc.UploadStringCompleted += new UploadStringCompletedEventHandler(wc_UploadStringCompleted);

    wc.UploadStringAsync(new Uri(access_token_url), "POST", GetPostParameters());


    void wc_UploadStringCompleted(object sender, UploadStringCompletedEventArgs e)
    {
        try 
        {
            MessageBox.Show(e.Result);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    string GetPostParameters()
    {
        string data = "";
        data += "client_id="+client_id + "&";
        data += "client_secret=" +client_secret + "&";
        data += "redirect_uri=" + redirect_uri+ "&";
        data += "code=" + App.AccessToken + "&";
        data += "grant_type=authorization_code";
        return data;
    }

有谁知道什么是错的?这让我发疯了,如果现在使用这种技术,那么oauth必须如此复杂才是真正的耻辱 .

提前致谢 .

1 回答

  • 0

    你能尝试对redirect_uri变量进行URL编码吗?

相关问题