騰訊云服務(wù)器 API C語(yǔ)言接口

騰訊云服務(wù)器的官方API和文檔非常詳盡,但是對(duì)于嵌入式設(shè)備還不是很友好,為了在項(xiàng)目中開(kāi)發(fā)一個(gè)發(fā)送短信的功能,寫了一個(gè)騰訊云服務(wù)器的客戶端api,抽取了其中框架部分:

騰訊云服務(wù)器 API處理的流程:

1)初始化變量,主要有三類:1 密鑰參數(shù),2 構(gòu)成請(qǐng)求頭(http head)的參數(shù),3 構(gòu)成請(qǐng)求數(shù)據(jù)(http body)的參數(shù)
2)生成請(qǐng)求數(shù)據(jù)(http body),請(qǐng)求數(shù)據(jù)的參數(shù)和請(qǐng)求數(shù)據(jù)生成的函數(shù)都是私有自定義
3)生成簽名的請(qǐng)求頭(http head),頭部參數(shù)和簽名頭部處理函數(shù)可以使用API提供的也可以參考API定義私有的
4)發(fā)送http post請(qǐng)求,并返回云服務(wù)器的響應(yīng)數(shù)據(jù)

具體處理Demo:發(fā)送短信,參見(jiàn)mian.c:

{
// 密鑰參數(shù)
const char* SECRET_ID = "AKID*******";    //cloud provider define
const char* SECRET_KEY = "oNg*******";    //cloud provider define

const char* testPhone2 = "+86133*******2";    //test value 2
const char* testVcode2 = "520520";            //test value 2

char *resp = NULL;

//1)new private parameter, private parameter are used by private callback func
struct defaultHeadParam *smsHead = smsHeadParamNew();
struct smsBodyParam *smsBody = smsBodyParamNew();

//2)new cloud api
struct cloudApi *smsApi = cloudApiNew();

if(!smsHead || !smsBody || !smsApi) return ERR_NOMEM;

//3)set cloud api id and key
cloudApiSecretSet(smsApi, SECRET_ID, SECRET_KEY);

//4)set private parameter and callback func
cloudApiHttpBodyInit(smsApi, smsBody, smsHttpBody);
cloudApiHttpHeadInit(smsApi, smsHead, defaultHttpSignatureHead);

//5)http post and return reply
resp = cloudApiHttpPost(smsApi);

//6)use reply value, do not need free it in you code
//just a demo, you can use cjson to handle resp here
if(resp){
   printf("%s\n", "resp1-----------------------");        
   printf("%s\n\n", resp);
}
//for test 2
//7)renew http body parameter demo(your private define if needed)
smsBodyPhoneVcodeRenew(smsBody, testPhone2, testVcode2);

//8)http post and return reply
resp = cloudApiHttpPost(smsApi);

if(resp){
   printf("%s\n", "resp2-----------------------");        
   printf("%s\n\n", resp);
}

//9)release
smsHeadParamFree(smsHead);
smsBodyParamFree(smsBody);
cloudApiFree(smsApi);

return ERR_SUCCESS;

}

注意:

1)私有定義的數(shù)據(jù)的內(nèi)存分配,API代碼不做資源維護(hù)處理,由你自己的代碼處理:
struct defaultHeadParam *smsHead = smsHeadParamNew();
struct smsBodyParam *smsBody = smsBodyParamNew();
smsHeadParamFree(smsHead);
smsBodyParamFree(smsBody);
2)cloudapi數(shù)據(jù)的內(nèi)存分配,由API維護(hù),外部?jī)H使用即可,在程序結(jié)束時(shí)顯式調(diào)用:
cloudApiFree(smsApi);
3)對(duì)外部庫(kù)的要求:
libcurl,版本不低于7.45.0:https://curl.haxx.se/download/curl-7.45.0.tar.bz2
選項(xiàng)注意:
1,--enable-ares dns解析使用異步庫(kù),避免網(wǎng)絡(luò)異常curl_easy_perform超時(shí),socket泄漏,參考:
gethostbyname阻塞分析 http://m.itdecent.cn/p/4c21e1f58f6e
2,當(dāng)在你的主程序中使用了openssl時(shí),在curl如果也選用openssl庫(kù)會(huì)出現(xiàn)異常,因此編譯curl庫(kù)建議選用其選項(xiàng)中的:

PolarSSL

3,可能因?yàn)閷?shí)際需要,你還需要集成cjson庫(kù)(序列化和反序列化http body),libiconv:將http body轉(zhuǎn)成utf-8
4)最常見(jiàn)的錯(cuò)誤:
{"Response":{"Error":{"Code":"AuthFailure.SignatureFailure","Message":"The provided credentials could not be validated. Please check your signature is correct."},"RequestId":"9ee538f4-1814-458a-a811-22ac0269dacd"}}
可以使用騰訊云提供的“簽名串生成”功能(登錄你的云賬號(hào)騰訊云提供了該調(diào)試功能)進(jìn)行調(diào)試,對(duì)比網(wǎng)頁(yè)生成的數(shù)據(jù)和代碼打印的數(shù)據(jù),找到不一致的地方進(jìn)行修改

因騰訊云API的一致性設(shè)計(jì)較好,該 API 框架應(yīng)該可以適用于其他騰訊云服務(wù)接口,希望能給有需要的人提供幫助,同時(shí)希望能夠得到使用者的優(yōu)化和改進(jìn)。

源碼地址:
https://github.com/xingchen02/qcloudapi

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容