C++ 获取Access Token函数返回成功,Access Token参数为空
李才标biao 发布于2017-08 浏览:4413 回复:3
0
收藏

使用官方C++ demo中的API获取Access Token,get_access_token函数返回成功,可是Access Token参数为空,这是什么原因?

代码如下:

#include

#include

#include "./include/curl/curl.h"

#include "./include/json/json.h"

//#include "access_token.h"

using namespace std;

// libcurl库下载链接:https://curl.haxx.se/download.html

// jsoncpp库下载链接:https://github.com/open-source-parsers/jsoncpp/

// 获取access_token所需要的url

const std::string access_token_url = "https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials";

// 回调函数获取到的access_token存放变量

//static std::string access_token_result;

/**

 * curl发送http请求调用的回调函数,回调函数中对返回的json格式的body进行了解析,解析结果储存在result中

 * @param 参数定义见libcurl库文档

 * @return 返回值定义见libcurl库文档

 */

static size_t callback(void *ptr, size_t size, size_t nmemb, void *stream) {

    // 获取到的body存放在ptr中,先将其转换为string格式

    std::string s((char *) ptr, size * nmemb);

    // 开始获取json中的access token项目

    Json::Reader reader;

    Json::Value root;

    // 使用boost库解析json

    reader.parse(s,root);

    std::string* access_token_result = static_cast(stream);

    *access_token_result = root["access_token"].asString();

    return size * nmemb;

}


/**

 * 用以获取access_token的函数,使用时需要先在百度云控制台申请相应功能的应用,获得对应的API Key和Secret Key

 * @param access_token 获取得到的access token,调用函数时需传入该参数

 * @param AK 应用的API key

 * @param SK 应用的Secret key

 * @return 返回0代表获取access token成功,其他返回值代表获取失败

 */

int get_access_token(std::string &access_token, const std::string &AK, const std::string &SK) {

    CURL *curl;

    CURLcode result_code;

    int error_code = 0;

    curl = curl_easy_init();

    if (curl) {

        std::string url = access_token_url + "&client_id=" + AK + "&client_secret=" + SK;

        curl_easy_setopt(curl, CURLOPT_URL, url.data());

        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);

        curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0);

        std::string access_token_result;

        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &access_token_result);

        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);

        result_code = curl_easy_perform(curl);

        if (result_code != CURLE_OK) {

            fprintf(stderr, "curl_easy_perform() failed: %s\n",

                    curl_easy_strerror(result_code));

            return 1;

        }

        access_token = access_token_result;

        curl_easy_cleanup(curl);

//cout<<"access_token_result"<

        error_code = 0;

    } else {

        fprintf(stderr, "curl_easy_init() failed.");

        error_code = 1;

    }

    return error_code;

}


int main(void)

{

//char AKey[20] = "abgfn";

//char AKey[20] = "abgfn";

//char SKey[20] = "abdgnaklg";

int ret = -1;

std::string APIKey ="6o7WPmwKjVBk7Bz80gbWgz1T";

std::string SecretKey = "ypBbZsfyGnoT7aC5iUqPcA6kQk1wCBV0 ";

std::string AccessKey;


ret = get_access_token(AccessKey, APIKey, SecretKey);

cout<<"ret="<

cout<<"SecretKey:"<

cout<<"AccessKey:"<

return 0;

}



编译和运行都没有任何的错误提示

biao@ubuntu:~/face$ make

g++ face.cpp ./lib/json/libjson_linux-gcc-5.4.0_libmt.a                                                                                       -o test -lcurl

biao@ubuntu:~/face$ ./test

ret=0

SecretKey:ypBbZsfyGnoT7aC5iUqPcA6kQk1wCBV0 

AccessKey:

biao@ubuntu:~/face$ 

请高人指点下:

收藏
点赞
0
个赞
共3条回复 最后由用户已被禁言回复于2022-04
#4追星逐月的家回复于2018-10

c++不适合这种网络开发,太麻烦

0
#3abccdj1011回复于2018-01

你好你的依赖库怎么解决的

 

 

 

 

1
#2李才标biao回复于2017-08

问题自己解决了    是我PC机的curl 安装有问题

0
TOP
切换版块