小编emi*_*001的帖子

是否有可能将CURLOPT_USERPWD转换为我可以在我的Android http客户端中粘贴在HTTP POST消息中的内容?

目前,我正在为Pinterest开发Android应用程序.我要做的是从pinterest(他们只是拉动他们的API页面)获取OAuth令牌.我发现有人的PHP脚本似乎允许某人使用以下方法登录:

function fetch_access_token($client_id, $client_secret, $username, $password) {
        $ch=curl_init();
        curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);                                                
        $post= array(
            "grant_type" => 'password',
            "scope"  => "read_write",
        );
        $host = "https://api.pinterest.com";
        $endpoint = "/oauth/2/access_token?client_id=$client_id&client_secret=$client_secret";
        $request_url = $host . $endpoint;
        curl_setopt($ch, CURLOPT_URL, $request_url);
        curl_setopt($ch, CURLOPT_POST,1);
        curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); 
        $s=curl_exec($ch);
        $info = curl_getinfo($ch);
        curl_close($ch);
        if ($info['http_code'] >= 200 and $info['http_code'] < 300) {
            list($junk, $access_token) = explode('=', $s, 2);
            $this->access_token = $access_token;
        } 
        return $s;
    }
Run Code Online (Sandbox Code Playgroud)

如您所见,在此函数的第三行中,CURLOPT_USERPWD用作curl post选项.我已经编写了一个HttpClient包装器库,我正用它来发出请求.我可以轻松添加HTTP POST参数,但是,我不知道要添加什么CURLOPT_USERPWD …

android http

2
推荐指数
1
解决办法
5428
查看次数

标签 统计

android ×1

http ×1