Моя рабочая среда такова, что библиотека libcurl установлена на встроенных устройствах linux. По какой-то причине мне пришлось использовать клавишу p12, чтобы выполнить работу, прочитать инструкции Google и другие ответы, и первым шагом было получить токен доступа, а затем использовать токен для доступа к облаку Google. Но как мне получить токен доступа с ключом p12 и библиотекой libcurl?
Я использовал инструмент openssl для получения файлов pem: openssl pkcs12 -in smart***.p12 -out client.pem -clcerts -nokeys
openssl pkcs12 -in smart***.p12 -out key.pem -nocerts
if((curl = curl_easy_init()) != NULL)
{
curl_slist *pOptionList = NULL;
pOptionList = curl_slist_append(pOptionList, "Content-Type: application/x-www-form-urlencoded");
pOptionList = curl_slist_append(pOptionList, "Expect:");
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, pOptionList);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 240L);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 60L);
curl_easy_setopt(curl, CURLOPT_PORT, 443L);
memset(url, 0, sizeof(url));
strncat(url, "https://oauth2.googleapis.com/token", 127);//url correct?
DEBUG_WARNING(" url = %s \n", url);
curl_easy_setopt(curl, CURLOPT_URL, url);
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, true);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 2L);
curl_easy_setopt(curl, CURLOPT_CAINFO, "roots.pem"); //public
curl_easy_setopt(curl, CURLOPT_SSLCERT, "client.pem"); //openssl pkcs12 -in smart***.p12 -out client.pem -clcerts -nokeys
curl_easy_setopt(curl, CURLOPT_SSLKEY, "key.pem"); //openssl pkcs12 -in smart***.p12 -out key.pem -nocerts
curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "xxxxxx"); //client define
curl_easy_setopt(curl, CURLOPT_POST, 1L);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, "grant_type=client_credentials");//fill param, correct?
CURLcode curlcode = curl_easy_perform(curl);
if(CURLE_OK != curlcode)
{
DEBUG_WARNING(" curlcode is not OK\n");
DEBUG_WARNING("Error: %s\n", curl_easy_strerror(curlcode));
curl_easy_cleanup(curl);
return -1;
}
int HTTP_res = -1;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &HTTP_res);
curl_slist_free_all(pOptionList);
curl_easy_cleanup(curl);
DEBUG_WARNING("Http response is %d\n", HTTP_res);
return HTTP_res;
}
Какполучить токен доступа? В настоящее время возвращает ошибку "error_description": "Invalid-grant-type: client-credentials"
Или каким-то другим способом. Благодаря.