Skip to content

Instantly share code, notes, and snippets.

@vanessaaleung
vanessaaleung / step1.c
Last active May 29, 2019 06:34
Step 1
/* curl_easy_setopt(handle, option, parameter): tells libcurl how to behave
/* specify URL to get */
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://accounts.google.com/o/oauth2/device/code");
char data[255];
strcat(data, "client_id=");
strcat(data, client_id);
strcat(data, "&scopescope=https://www.googleapis.com/auth/drive.file");
int main(void)
{
CURL *curl_handle;
CURLcode res;
struct MemoryStruct chunk;
chunk.memory = malloc(1); /* will be grown as needed by the realloc above */
chunk.size = 0; /* no data at this point */
@vanessaaleung
vanessaaleung / client_id.c
Last active May 29, 2019 07:54
client id
char * client_id = "YOUR_CLIENT_ID";
char * client_secret = "YOUR_CLIENT_SECRET";
@vanessaaleung
vanessaaleung / sampleresponse.json
Last active May 29, 2019 06:51
sampleresponse
{
"device_code" : "4/4-GMMhmHCXhWEzkobqIHGG_EnNYYsAkukHspeYUk9E8",
"user_code" : "GQVQ-JKEC",
"expires_in" : 1800,
"interval" : 5,
"verification_url" : "https://www.google.com/device"
}
char user_code[255];
char device_code[255];
/* check for errors */
if(res != CURLE_OK) {
fprintf(stderr, "curl_easy_perform() failed: %s\n", /* stderr: default destination for error messages */
curl_easy_strerror(res)); /* returns a string describing the error code */
}
else {
/* Now, our chunk.memory points to a memory block that is chunk.size
@vanessaaleung
vanessaaleung / step3.c
Last active May 29, 2019 07:05
Step 3
printf("Enter below code on 'https://www.google.com/device' :\n");
printf("%s\n", user_code);
/* Instruct user to press `Enter` when finish verification process */
printf("Press enter when finish\n");
/* When detect a new line, continue */
while(getchar()!= '\n');
chunk.memory = malloc(1);
chunk.size = 0;
curl_easy_setopt(curl_handle, CURLOPT_URL, "https://www.googleapis.com/oauth2/v4/token");
char url[300];
strcat(url, "client_id=");
strcat(url, client_id);
strcat(url, "&client_secret=");
strcat(url, client_secret);
@vanessaaleung
vanessaaleung / sampleresponse2.json
Created May 29, 2019 07:29
Sample Response in Step 4
{
"access_token":"1/fFAGRNJru1FTz70BzhT3Zg",
"expires_in":3920,
"token_type":"Bearer",
"refresh_token":"1/xEoDL4iW3cxlI7yDbSRFYNG01kVKM2C-259HOF2aQbI"
}
char access_token[255];
char access_token[255];
if(res != CURLE_OK){
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
}
else{
char *expiresin_result2 = strstr(chunk.memory, "\"expires_in");
int expiresin_position2 = (int)(expiresin_result2 - chunk.memory);
char *accesstoken_result = strstr(chunk.memory, "\"access_token");