file-type

使用libcurl和libjson获取人人网access_token和session_key教程

4星 · 超过85%的资源 | 下载需积分: 50 | 1.29MB | 更新于2025-04-30 | 113 浏览量 | 39 下载量 举报 收藏
download 立即下载
### 知识点 #### 1. libcurl的使用 libcurl是一个客户端的URL传输库,支持多种协议,包括HTTP、HTTPS、FTP等。它被广泛用于执行文件传输任务,可以用于多种不同的编程语言,包括C、C++等。在本案例中,libcurl将被用来与人人网API进行通信,以获取access_token和session_key。 使用libcurl时,开发者通常需要初始化一个CURL句柄,然后设置请求的URL和相关的选项,如请求类型(GET或POST)、HTTP头部、POST数据等。完成设置后,通过curl_easy_perform()函数来执行实际的HTTP请求。请求完成之后,开发者可以从CURL句柄中提取响应数据。 #### 2. libjson的使用 libjson是一个用于处理JSON格式数据的C++库。JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写,也易于机器解析和生成。它常用于网络通信中数据的传输格式。 libjson库提供了简单的接口来解析和序列化JSON数据,使得开发者可以在C++程序中轻松地创建、修改和访问JSON对象。在本案例中,libjson将被用来解析从人人网API返回的JSON格式的响应数据,提取出access_token和session_key。 #### 3. 人人网API 人人网(现更名为“人人”),是中国的一个社交网络服务网站。网站提供包括社交网络、游戏、音乐等多种服务。在本案例中,将要使用人人网开放平台的API进行开发者身份验证和数据交互。 开发者需要在人人网开放平台注册并创建应用,获取对应的APP Key。有了APP Key之后,开发者可以调用人人网的API来请求access_token和session_key,这两个是用于后续API调用的凭证。 #### 4. 获取access_token和session_key 为了获取access_token和session_key,开发者需要使用APP Key向人人网的认证服务器发送一个HTTP请求。请求中通常会包含APP Key和密钥(APP Secret),以及指定的授权码(在用户授权后获得)。 一旦人人网的服务器验证了APP Key和密钥,并且授权码有效,服务器就会返回一个包含access_token和session_key的响应。开发者可以将这些信息保存下来,并在后续API调用中使用这些凭证来访问用户数据等资源。 #### 5. C++ SDK和main.cpp文件 CPP SDK指的是为人人网提供的C++开发者工具包。这个工具包可能包含了一系列的API接口定义、请求封装、响应处理等,以及相关的示例代码,以便开发者可以快速地接入人人网API进行开发。 在提供的文件信息中,提到的main.cpp文件是C++项目的入口文件,开发者需要在其中编写代码实现功能逻辑。开发者可能会在这里使用libcurl发起网络请求,并使用libjson解析返回的JSON数据,以获取session_key和access_token。 #### 6. 编译和调试 Debug是压缩包子文件的文件名称列表中提到的内容,可能是指项目的调试版本。通常,开发者需要对代码进行编译,并生成可执行文件。在开发过程中,利用调试版本的可执行文件,开发者可以使用调试器检查程序运行状态,跟踪代码执行流程,监视变量值,从而更有效地定位和解决程序中出现的问题。 ### 总结 综上所述,为了使用libcurl和libjson从人人网获取access_token和session_key,开发者需要了解相关网络编程的知识、掌握如何使用libcurl发送HTTP请求和libjson处理JSON数据。获取凭证后,这些信息将被集成到C++ SDK中,以便在main.cpp文件中实现身份验证和后续的数据操作。在开发过程中,调试是不可或缺的一环,有助于确保程序的正确性和稳定性。

相关推荐

filetype

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <curl/curl.h> #include "cJSON.h" #include "depskmainCopy.h" #include "lvgl/lvgl.h" #include <stdio.h> #include <errno.h> #include "ui_helpers.h" #include "ui_events.h" #include "ui.h" #include "ui_helpers.h" // 使用缓冲区输入输出是没问题的,需要解决循环的问题, char textbuf[128] = "who are you"; extern char * kbEntertext; char dpOut[128] = {0}; // ================== 前置声明 ================== struct MemoryStruct; struct APIResponse; char* get_api_key(); struct APIResponse call_deepseek(const char *api_key, const char *prompt); void print_token_usage(int prompt_tokens, int completion_tokens, int total_tokens); DeepSeekSession *session; char input[1024]; int prompt_tokens, completion_tokens, total_tokens; char *response; // ================== 辅助函数 ================== // libcurl回调函数 static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp) { size_t realsize = size * nmemb; struct MemoryStruct *mem = (struct MemoryStruct *)userp; char *ptr = realloc(mem->memory, mem->size + realsize + 1); if(!ptr) { fprintf(stderr, "内存分配错误\n"); return 0; } mem->memory = ptr; memcpy(&(mem->memory[mem->size]), contents, realsize); mem->size += realsize; mem->memory[mem->size] = 0; return realsize; } // 获取API密钥 char* get_api_key() { char *api_key = getenv("DEEPSEEK_API_KEY"); if (api_key && strlen(api_key) > 0) { return strdup(api_key); } printf("未找到环境变量 DEEPSEEK_API_KEY\n"); printf("请访问 https://platform.deepseek.com/api-keys 创建API密钥\n"); char *input_key = malloc(256); if (!input_key) { fprintf(stderr, "内存分配失败\n"); return NULL; } printf("请输入您的DeepSeek API密钥: "); if (fgets(input_key, 256, stdin) == NULL) { free(input_key); return NULL; } input_key[strcspn(input_key, "\n")] = 0; return input_key; } // ================== 核心API函数 ================== // 调用DeepSeek API struct APIResponse call_deepseek(const char *api_key, const char *prompt) { CURL *curl; CURLcode res; struct MemoryStruct chunk = {0}; long http_code = 0; struct APIResponse api_response = {0}; curl_global_init(CURL_GLOBAL_DEFAULT); curl = curl_easy_init(); if(!curl) { fprintf(stderr, "无法初始化cURL\n"); return api_response; } // 设置API端点 curl_easy_setopt(curl, CURLOPT_URL, "https://api.deepseek.com/chat/completions"); // 设置证书路径 // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // // 添加 SSL 版本和密码套件配置 // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "HIGH:!aNULL:!MD5"); // 设置 TLS 1.3 // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_3); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "TLS_AES_128_GCM_SHA256"); // curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); // curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "DEFAULT@SECLEVEL=1"); // 添加SSL证书验证配置,禁用证书验证(仅用于测试) // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); // 设置完整的证书链 // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // curl_easy_setopt(curl, CURLOPT_CAPATH, "/etc/ssl/certs"); // 配置SSL证书和验证选项 curl_easy_setopt(curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1_2); curl_easy_setopt(curl, CURLOPT_SSL_CIPHER_LIST, "DEFAULT"); curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/new_combined_certs.pem"); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // 验证配置 // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L); // curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L); // curl_easy_setopt(curl, CURLOPT_CAINFO, "/etc/ssl/certs/ca-certificates.crt"); // 添加更多调试信息 // curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); // 添加详细的错误信息 char errbuf[CURL_ERROR_SIZE]; curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errbuf); curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); curl_easy_setopt(curl, CURLOPT_CERTINFO, 1L); // 设置请求头 struct curl_slist *headers = NULL; headers = curl_slist_append(headers, "Content-Type: application/json"); char auth_header[256]; snprintf(auth_header, sizeof(auth_header), "Authorization: Bearer %s", api_key); headers = curl_slist_append(headers, auth_header); curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers); // 构建请求体 cJSON *root = cJSON_CreateObject(); cJSON_AddStringToObject(root, "model", "deepseek-chat"); cJSON_AddNumberToObject(root, "max_tokens", 2000); cJSON *messages = cJSON_CreateArray(); cJSON *message = cJSON_CreateObject(); cJSON_AddStringToObject(message, "role", "user"); cJSON_AddStringToObject(message, "content", prompt); cJSON_AddItemToArray(messages, message); cJSON_AddItemToObject(root, "messages", messages); char *post_data = cJSON_PrintUnformatted(root); // 设置POST数据 curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (long)strlen(post_data)); // 设置响应回调 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)&chunk); // 设置超时 curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); // 执行请求 res = curl_easy_perform(curl); // 检查不支持的协议 if(res == CURLE_UNSUPPORTED_PROTOCOL) { fprintf(stderr, "cURL错误: 不支持的协议 - URL: %s\n", "https://api.deepseek.com/chat/completions"); // 获取支持的协议列表 curl_version_info_data *ver_info = curl_version_info(CURLVERSION_NOW); if(ver_info && ver_info->protocols) { fprintf(stderr, "支持的协议列表:\n"); const char * const *proto; for(proto = ver_info->protocols; *proto; proto++) { fprintf(stderr, "- %s\n", *proto); } } } else if(res != CURLE_OK) { fprintf(stderr, "cURL错误: %s\n", curl_easy_strerror(res)); fprintf(stderr, "详细错误: %s\n", errbuf); long connect_code; curl_easy_getinfo(curl, CURLINFO_HTTP_CONNECTCODE, &connect_code); fprintf(stderr, "连接状态码: %ld\n", connect_code); } else if (res == CURLE_SSL_PEER_CERTIFICATE) { fprintf(stderr, "SSL证书错误: %s\n", curl_easy_strerror(res)); // 获取更多错误信息 char error_buffer[CURL_ERROR_SIZE]; curl_easy_getinfo(curl, CURLINFO_SSL_VERIFYRESULT, &error_buffer); fprintf(stderr, "SSL验证结果: %s\n", error_buffer); } // 检查HTTP状态码 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code); // 清理cURL资源 curl_easy_cleanup(curl); curl_slist_free_all(headers); cJSON_Delete(root); free(post_data); // 调试信息 printf("\n--- API请求详情 ---\n"); printf("端点: https://api.deepseek.com/chat/completions\n"); printf("模型: deepseek-chat\n"); printf("提示: %s\n", prompt); printf("状态码: %ld\n", http_code); if(res != CURLE_OK) { fprintf(stderr, "cURL错误: %s\n", curl_easy_strerror(res)); if(chunk.memory) free(chunk.memory); curl_global_cleanup(); return api_response; } // 检查HTTP状态码 if(http_code != 200) { fprintf(stderr, "API错误: HTTP %ld\n", http_code); if(chunk.memory) { printf("原始响应: %s\n", chunk.memory); cJSON *error_json = cJSON_Parse(chunk.memory); if(error_json) { cJSON *error_obj = cJSON_GetObjectItem(error_json, "error"); if(error_obj) { cJSON *message = cJSON_GetObjectItem(error_obj, "message"); cJSON *code = cJSON_GetObjectItem(error_obj, "code"); if(cJSON_IsString(message)) { fprintf(stderr, "错误信息: %s\n", message->valuestring); } if(cJSON_IsString(code)) { fprintf(stderr, "错误代码: %s\n", code->valuestring); } } cJSON_Delete(error_json); } } if(chunk.memory) free(chunk.memory); curl_global_cleanup(); return api_response; } curl_global_cleanup(); // 解析API响应 cJSON *response_json = cJSON_Parse(chunk.memory); if (!response_json) { const char *error_ptr = cJSON_GetErrorPtr(); if (error_ptr) fprintf(stderr, "JSON解析错误: %s\n", error_ptr); fprintf(stderr, "原始响应: %s\n", chunk.memory); free(chunk.memory); return api_response; } // 提取令牌使用信息 cJSON *usage = cJSON_GetObjectItem(response_json, "usage"); if (usage) { cJSON *prompt_tokens = cJSON_GetObjectItem(usage, "prompt_tokens"); cJSON *completion_tokens = cJSON_GetObjectItem(usage, "completion_tokens"); cJSON *total_tokens = cJSON_GetObjectItem(usage, "total_tokens"); if (cJSON_IsNumber(prompt_tokens)) api_response.prompt_tokens = prompt_tokens->valueint; if (cJSON_IsNumber(completion_tokens)) api_response.completion_tokens = completion_tokens->valueint; if (cJSON_IsNumber(total_tokens)) api_response.total_tokens = total_tokens->valueint; } // 提取回复内容 cJSON *choices = cJSON_GetObjectItem(response_json, "choices"); if (cJSON_IsArray(choices) && cJSON_GetArraySize(choices) > 0) { cJSON *first_choice = cJSON_GetArrayItem(choices, 0); cJSON *message = cJSON_GetObjectItem(first_choice, "message"); cJSON *content = cJSON_GetObjectItem(message, "content"); if (cJSON_IsString(content) && content->valuestring != NULL) { api_response.content = strdup(content->valuestring); } } cJSON_Delete(response_json); free(chunk.memory); return api_response; } // 显示令牌使用信息 void print_token_usage(int prompt_tokens, int completion_tokens, int total_tokens) { printf("\n--- 令牌使用 ---\n"); printf("提示令牌: %d\n", prompt_tokens); printf("完成令牌: %d\n", completion_tokens); printf("总令牌: %d\n", total_tokens); // 估算成本 double estimated_cost = total_tokens * 0.0000015; // $0.0015/1K tokens printf("估算成本: $%.6f\n", estimated_cost); } // ================== 会话管理函数 ================== // 创建新会话 DeepSeekSession* deepseek_create_session(const char *api_key) { DeepSeekSession *session = malloc(sizeof(DeepSeekSession)); if (!session) return NULL; // 修复类型不匹配问题 if (api_key) { session->api_key = strdup(api_key); } else { session->api_key = get_api_key(); } session->total_prompt_tokens = 0; session->total_completion_tokens = 0; session->total_all_tokens = 0; return session; } // 销毁会话 void deepseek_destroy_session(DeepSeekSession *session) { if (!session) return; free(session->api_key); free(session); } // 发送消息并获取回复 char* deepseek_send_message( DeepSeekSession *session, const char *message, int *prompt_tokens, // 返回本次提示令牌 int *completion_tokens, // 返回本次完成令牌 int *total_tokens // 返回本次总令牌 ) { if (!session || !message) return NULL; struct APIResponse response = call_deepseek(session->api_key, message); if (response.content) { // 更新会话令牌计数 session->total_prompt_tokens += response.prompt_tokens; session->total_completion_tokens += response.completion_tokens; session->total_all_tokens += response.total_tokens; // 设置输出参数 if (prompt_tokens) *prompt_tokens = response.prompt_tokens; if (completion_tokens) *completion_tokens = response.completion_tokens; if (total_tokens) *total_tokens = response.total_tokens; return response.content; } return NULL; } // 获取会话令牌统计 void deepseek_get_token_stats( DeepSeekSession *session, int *total_prompt, int *total_completion, int *total_all ) { if (!session) return; if (total_prompt) *total_prompt = session->total_prompt_tokens; if (total_completion) *total_completion = session->total_completion_tokens; if (total_all) *total_all = session->total_all_tokens; } void get_full_text_input(char *dest, size_t dest_size) { // 获取文本区域内容指针 const char *text = lv_textarea_get_text(ui_TextArea1); if (!text) { dest[0] = '\0'; return; } // 计算实际文本长度(包括空格) size_t text_len = strlen(text); // 安全复制文本(防止缓冲区溢出) size_t copy_len = text_len < dest_size - 1 ? text_len : dest_size - 1; strncpy(dest, text, copy_len); dest[copy_len] = '\0'; // 确保字符串终止 // 移除末尾的换行符(如果有) char *newline = strchr(dest, '\n'); if (newline) *newline = '\0'; printf("111111111111\n"); } // ================== 主函数 ================== int depmainlong(lv_event_t * e) // 创建会话 { lv_event_code_t event_code = lv_event_get_code(e); lv_obj_t * target = lv_event_get_target(e); if( event_code == LV_EVENT_LONG_PRESSED ) { // 创建会话 - 修复API密钥传递问题 // session = deepseek_create_session("sk-28b778879e5b4fd6b227d767812fd83d"); // if (!session) { // fprintf(stderr, "创建会话失败\n"); // return 1; // } printf("DeepSeek聊天客户端 (输入'exit'退出)\n"); printf("使用的API密钥: %.6s...\n", session->api_key); // if( !lv_obj_has_flag(ui_Keyboard1, LV_OBJ_FLAG_HIDDEN)) { // printf("\n你的问题: "); // // char input[1024]; // // 如何判定提交文本时机,以及如何控制循环 // /********************* // * @brief Construct a new if object // * @details 按键长按不起作用,起作用的是点击 // * 点击一次,进入一次,判断一次。 // * 而由于全局变量的设置,文本内容与对话的循环并无直接关系 // * 目前会出现段错误,猜测可能是由于内容缓冲区越界,因为还保留了上下文 // *************************************************/ // if( event_code == LV_EVENT_CLICKED ) // { // printf("inputlogo被长按!\n"); // printf("111111111111"); // } // if(1) // { // kbEntertext = lv_textarea_get_text(ui_TextArea1); // printf("提交内容: %s\n", kbEntertext); // } // // 使用这种方式需要处理循环的问题以及刷新。 // sscanf(kbEntertext, "%s", input); // printf("kbEntertext = %s\n", kbEntertext); // printf("input = %s\n", input); // // 移除换行符 // input[strcspn(input, "\n")] = 0; // // 线程阻塞原因,暂不使用循环while 以及相应的语句 // // if(strcmp(input, "exit") == 0) break; // // if(strlen(input) == 0) continue; // printf("正在查询DeepSeek API...\n"); // // int prompt_tokens, completion_tokens, total_tokens; // // char *response = deepseek_send_message // response = deepseek_send_message( // session, // input, // &prompt_tokens, // &completion_tokens, // &total_tokens // ); // if(response) { // // printf("\n--- DeepSeek回复 ---\n%s\n", response); // printf("\n--- DeepSeek回复 ---\n"); // sprintf(dpOut, "%s", response); // printf("%s\n", dpOut); // lv_label_set_text(ui_AILabel, dpOut); // // 显示令牌使用 // print_token_usage(prompt_tokens, completion_tokens, total_tokens); // free(response); // } else { // printf("获取回复时出错\n"); // } // } return 0; } } int depmaintalk(lv_event_t * e) // 会话 { lv_event_code_t event_code = lv_event_get_code(e); lv_obj_t * target = lv_event_get_target(e); if( event_code == LV_EVENT_CLICKED ) { if( !lv_obj_has_flag(ui_Keyboard1, LV_OBJ_FLAG_HIDDEN)) { printf("\n你的问题: "); // char input[1024]; printf("inputlogo被anxia!\n"); // kbEntertext = lv_textarea_get_text(ui_TextArea1); // input = lv_textarea_get_text(ui_TextArea1); get_full_text_input(input, sizeof(input)); // sscanf(lv_textarea_get_text(ui_TextArea1), "%s", input); printf("kbEntertext = %s\n", kbEntertext); printf("input = %s\n", input); if( strcmp(input, "exit") == 0 ) { // 清理资源 deepseek_destroy_session(session); printf("退出!\n"); return 0; } // 使用这个清理判断会卡住,不知道为什么 // if( strcmp(input, "exit") ) // { // // 清理资源 // deepseek_destroy_session(session); // return 0; // } printf("正在查询DeepSeek API...\n"); // char *response = deepseek_send_message response = deepseek_send_message( session, input, &prompt_tokens, &completion_tokens, &total_tokens ); if(response) { // printf("\n--- DeepSeek回复 ---\n%s\n", response); printf("\n--- DeepSeek回复 ---\n"); sprintf(dpOut, "%s", response); printf("%s\n", dpOut); lv_label_set_text(ui_AILabel, dpOut); // 显示令牌使用 print_token_usage(prompt_tokens, completion_tokens, total_tokens); free(response); } else { printf("获取回复时出错\n"); } } if( strcmp(input, "exit") == 0 ) { // 清理资源 deepseek_destroy_session(session); return 0; } } }

NetW0rk3r
  • 粉丝: 8
上传资源 快速赚钱