int main(int argc, char *argv[]) { json_struct *json_data = (json_struct *) malloc( sizeof(json_struct) ); char *json_file_path = (char *) malloc( sizeof(char) * 128 ); json_file_path = strcpy( json_file_path, getenv("HOME") ); json_file_path = strcat( json_file_path, JSON_FILENAME ); /*check if already have an access token*/ if( access(json_file_path, R_OK) == -1 ){ char *url = (char *) malloc( sizeof(char) * URL_LEN ); char *code = (char *) malloc( sizeof(char) * URL_LEN ); url = obtain_auth_code_url( url ); printf("Copy this link and paste it into your browser:\n%s\n\n",url); printf("Paste the code you obtained:\n"); while( scanf("%s",code) != 1 ) printf("Input a correct code you obtained by the supplied link!\n"); if( !(json_data = exchange_code_for_token( code, json_data, json_file_path) ) ) exit(1); free(url); free(code); }else{ if( ! ( json_data = get_access_token(json_data, json_file_path) ) ) exit(1); } free(json_file_path); free(json_data); return 0; }
pplx::task<utility::string_t> upload_to_dropbox( const utility::string_t &localFile, const utility::string_t &remoteFileName) { // Open stream to file to upload. return file_stream<uint8_t>::open_istream(localFile).then([=](basic_istream<uint8_t> fileStream) { // For more information about the Dropbox API see https://www.dropbox.com/developers/core/docs http_client client(U("https://api-content.dropbox.com/1/")); http_request req(methods::PUT); // Use /files_put API uri_builder builder(U("/files_put/auto")); builder.append_path(remoteFileName); builder.append_query(U("access_token"), get_access_token()); req.set_request_uri(builder.to_uri()); // Set file stream as the request body. req.set_body(fileStream); // Send request, return json result as a string. return client.request(req).then([](http_response response) { return response.extract_json(); }).then([](const json::value &result) { return result.serialize(); }); }); }
/** * @brief Use given API key to get the permanent key pair as the * initialization step of plurk * * @param req the address of API key pair * @param permanent the address of permanent key pair * * @return error on 1 */ int plurk_init(key_pair* req, key_pair* permanent){ // prepare for response key key_pair* tmp = malloc(sizeof(key_pair)); tmp->key = NULL; tmp->secret = NULL; // prepare for authorize verified code char* verifier_code = malloc(sizeof(char) * 8); // OAuth prcess procedure // 1. get response key // 2. get the verifier code // 3. get the permanent access key get_response_token(req->key, req->secret, &(tmp->key), &(tmp->secret)); get_verifier(req->key, req->secret, tmp->key, tmp->secret, &verifier_code); get_access_token(req->key, req->secret, &(tmp->key), &(tmp->secret), verifier_code); // store the permanent key/secret permanent->key = tmp->key; permanent->secret = tmp->secret; // free the dynamic allocated space free(verifier_code); //free(tmp->key); // << save for outer, no free //free(tmp->secret); // << save for outer, no free return 0; }
void VkAuth::check_load_page(bool ok) { common::log((ok ? "ok" : "fail")); switch (step) { case LOGIN_STEP: set_auth_field(); break; case AGREE_STEP: agree_on_access(); break; case GET_ACCESS_TOKEN_STEP: get_access_token(); break; } }
int main(int argc, char *argv[]) { int rc = 0, first = 1, c = 0, i = 0, l = 0; size_t length = 0; char *exec = NULL, *arg = NULL; char *client_id = NULL, *client_secret = NULL, *access_token = NULL; char *max_id = NULL, *id = NULL, *link = NULL; exec = argv[0]; if (argc < 2) { usage(exec); rc = 1; goto cleanup; } for (i = 1, l = argc; i < l; i++) { arg = argv[i]; length = strlen(arg); if (strncmp(arg, "--client_id", 11) == 0) { length -= 12; client_id = malloc(sizeof(char) * (length + 1)); strncpy(client_id, arg + 12, length); client_id[length] = '\0'; } if (strncmp(arg, "--client_secret", 15) == 0) { length -= 16; client_secret = malloc(sizeof(char) * (length + 1)); strncpy(client_secret, arg + 16, length); client_secret[length] = '\0'; } } if (get_access_token(&access_token, client_id, client_secret) != 0) { printf("Unable to get access_token\n"); rc = 1; goto cleanup; } draw_init(); while (1 == TRUE) { c = getch(); draw_border(); if ((photo_pos == 0 && first == 1) || photo_pos == photo_count - 1) { get_feed(access_token, &max_id); draw_instagram(&id, &link); if (first == 1) { first = 0; } } switch (c) { case '\033': getch(); c = getch(); switch (c) { case 'A': photo_pos--; if (photo_pos < 0) { photo_pos = 0; } break; case 'B': photo_pos++; if (photo_pos >= MAX) { goto cleanup; } break; default: break; } draw_instagram(&id, &link); break; case 'o': open_url(link); break; case 'q': goto cleanup; break; default: break; } } cleanup: draw_clear(); draw_end(); if (client_id) { free(client_id); } if (client_secret) { free(client_secret); } if (access_token) { free(access_token); } if (id) { free(id); } if (link) { free(link); } return rc; }
int file_finder(api_config *config, char *path, char *parent_dir) { BYTE *bytes; DWORD size; photo_linked_list *temp, *head, *killme, *last; char *album_id; DIR *dp; struct stat st; struct dirent *dptr; char filename[DIRPATHSIZE]; char **dirs_array; char **files_array; int dirs_array_size, files_array_size, dirs_index, files_index, i, upload_tries; dptr = NULL; upload_tries = 0; head = (photo_linked_list*) malloc(sizeof(photo_linked_list)); memset(head->photo_path, 0, DIRPATHSIZE); head->next = NULL; dp = NULL; last = head; dirs_array_size = files_array_size = dirs_index = files_index = 0; throw_error(NULL == (dp = opendir(path)), CANT_OPEN_DIR, "Could not open %s", path); log_info("Entering %s", path); while(NULL != (dptr = readdir(dp))) { if(0 == strcmp(".", dptr->d_name)) continue; if(0 == strcmp("..", dptr->d_name)) continue; strncpy(filename, path, DIRPATHSIZE); strncat(filename, PATH_SEPARATOR, DIRPATHSIZE); strncat(filename, dptr->d_name, DIRPATHSIZE); stat(filename, &st); if(S_ISDIR(st.st_mode)) { strcpy(last->photo_path, filename); last->type = 0x01; dirs_array_size++; last->next = (photo_linked_list*) malloc(sizeof(photo_linked_list)); last->next->next = NULL; last = last->next; } else { strcpy(last->photo_path, filename); last->type = 0x02; files_array_size++; last->next = (photo_linked_list*) malloc(sizeof(photo_linked_list)); last->next->next = NULL; last = last->next; } } closedir(dp); temp = head; dirs_array = malloc(sizeof(char*) * dirs_array_size); files_array = malloc(sizeof(char*) * files_array_size); while(NULL != temp->next) { if(0x01 == temp->type) { dirs_array[dirs_index] = malloc(sizeof(char) * strlen(temp->photo_path) + 1); strcpy(dirs_array[dirs_index], temp->photo_path); dirs_index++; } if(0x02 == temp->type) { files_array[files_index] = malloc(sizeof(char) * strlen(temp->photo_path) + 1); strcpy(files_array[files_index], temp->photo_path); files_index++; } killme = temp; temp = temp->next; free(killme); } free(temp); qsort(dirs_array, dirs_array_size, sizeof(char*), compare_strings); qsort(files_array, files_array_size, sizeof(char*), compare_strings); for(i=0; i<dirs_array_size; i++) { throw_error(!file_finder(config, dirs_array[i], &dirs_array[i][strlen(path) + sizeof(char)]), last_error, "problem calling file_finder"); free(dirs_array[i]); } for(i=0; i<files_array_size; i++) { log_info("running (%s): %s", parent_dir, files_array[i]); if(NULL == parent_dir) { log_info("=> No album. File %s should be uploaded without a album", files_array[i]); album_id = NULL; } else { throw_error(!get_album_by_name(config, parent_dir, &album_id), APP_ERROR, "problem calling get_album_by_name for %s", parent_dir); if(NULL == album_id) { log_info("=> Album %s doesn't exist. We must create it", parent_dir); throw_error(!create_new_album(config, parent_dir), APP_ERROR, "could not create new album %s", parent_dir); throw_error(!get_album_by_name(config, parent_dir, &album_id), APP_ERROR, "problem calling get_album_by_name for %s", parent_dir); throw_error(NULL == album_id, APP_ERROR, "Album was not created or could not be found. Must exit"); } else { log_info("=> Album %s found. It's id is %s. Let's upload photo %s to album id %s", parent_dir, album_id, files_array[i], album_id); } } throw_error(!resize_jpeg(files_array[i], &bytes, &size), APP_ERROR, "Problem calling resize_jpeg on %s", files_array[i]); log_info("Begin upload %s", files_array[i]); while(!upload_photo_stream_to_album(config, bytes, size, album_id)) { throw_error(upload_tries++ > 2, APP_ERROR, "too many tries to upload image"); if(OUTDATE_ACCESS_TOKEN == last_error) { log_warn("access_token esta desatualizado"); throw_error(!get_access_token(config), APP_ERROR, "error calling get_access_token"); } } log_info("Ended upload %s", files_array[i]); free(bytes); throw_error(unlink(files_array[i]) < 0, APP_ERROR, "could not delete file %s", files_array[i]); free(files_array[i]); } if(NULL != parent_dir) rmdir(path); free(dirs_array); free(files_array); return 1; error: return 0; }
int main() { DIR *dirtry = NULL; api_config config_data; curl_version_info_data *curl_info_data; curl_global_init(CURL_GLOBAL_ALL); init_idname_mylist(); FreeImage_Initialise(TRUE); FreeImage_SetOutputMessage(FreeImageErrorHandler); curl_info_data = curl_version_info(CURLVERSION_NOW); log_info("Imgur Folder2Album Uploader " VERSION_NUMBER); log_info("Using Curl %s", curl_info_data->version); log_info("Using FreeImage %s", FreeImage_GetVersion()); memset(&config_data, 0, sizeof(api_config)); if(!get_config_from_file(&config_data)) { if(CONFIG_FILE_DOESNOT_EXIST == last_error) { if(!get_imgur_pin(&config_data)) { return 1; } if(!get_access_token(&config_data)) { return 1; } if(!save_config_data(&config_data)) { return 1; } } } log_info("Welcome %s", config_data.account_username); if(!load_idname() && CANT_OPEN_FILE == last_error) { log_info("File " ALBUM_DATA_FILE " does not exist."); } while(NULL == (dirtry = opendir(config_data.images_directory))) { log_err("Could not open dir %s\n", config_data.images_directory); get_imgur_dir(&config_data); } closedir(dirtry); if(!save_config_data(&config_data)) { return 1; } file_finder(&config_data, config_data.images_directory, NULL); curl_global_cleanup(); FreeImage_DeInitialise(); if(!save_config_data(&config_data)) { return 1; } if(!save_idname()) { return 1; } // print_albuns(); kill_idname_mylist(); log_info("Bye"); return 0; }