我在c中使用liboauth库来验证我的gmail帐户的smtp通信。以下uri是我获取访问刷新和访问令牌的请求:
https://accounts.google.com/o/oauth2/auth?oauth_nonce=JtSsdRUwerfiILDiCmMrzRV1&oauth_signature_method=HMAC-SHA1&oauth_timestamp=1472540176&oauth_version=1.0&oauth_signature=%2BeJ8L%2BN63cqtkQvjrojDfoe8CSY%3D&client_id=5499633543492-47g4j52ikumagml1ka7pjemi91m79045.apps.googleusercontent.com&response_type=code&scope=https://mail.google.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob
我可以通过浏览器中的地址进行身份验证,但无法通过以下c代码进行身份验证。
以下代码段(使用示例客户端ID和客户端密钥)连接到oauth,并返回custormer_id
和secret_key
。
const char *request_token_uri ="https://accounts.google.com/o/oauth2/auth";
const char *req_c_key = "s3rsdfsd-87g4j52ikuemsdjemsdfm79045.apps.googleusercontent.com"; //< consumer key
const char *req_c_secret = "42sdfsdf3KsdfkKHBTiW5L"; //< consumer secret
char *res_t_key = NULL; //< replied key
char *res_t_secret = NULL; //< replied secret
char *req_url = NULL;
char *reply;
oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL, req_c_key, req_c_secret, res_t_key, res_t_secret);
char * new_str ;
if((new_str = malloc(strlen(req_url)+strlen("&client_id=54s3251492-8sdsdf9045.apps.googleusercontent.com&response_type=code&scope=https://mail.google.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob")+1)) != NULL){
new_str[0] = '\0'; // ensures the memory is an empty string
strcat(new_str,req_url);
strcat(new_str,"&client_id=234sdfser-87sdffsdfsdf5.apps.googleusercontent.com&response_type=code&scope=https://mail.google.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob\0");
}
else
{
printf("malloc failed!\n");
// exit?
}
printf("Get:result key from get op:%s\n\n",res_t_key);
reply = oauth_http_get(new_str,NULL);
printf("reply:%s",reply);
我明白了:
"HTTP request for an oauth request-token failed"