示例#1
0
static void gsocial_send_request(Session *request)
{
    request->exit_code = 0;
    char *escaped_tweet = NULL;
    int is_post = 0;
    char endpoint[500];
    char *req_url;
    char *reply;
    char *postarg = NULL;
    switch(request->action) {
        case ACTION_HOME_TIMELINE:
            if(request->since_id != NULL) {
                sprintf(endpoint, "%s%s?%s%s", twitter_statuses, "/home_timeline.xml", 
                        "since_id=", request->since_id);
                printf ( "%s\n", request->since_id );
            }
            else {
                sprintf(endpoint, "%s%s", twitter_statuses, "/home_timeline.xml");
            }
            break;
        case ACTION_UPDATE:
            escaped_tweet = oauth_url_escape(request->tweet);
            sprintf(endpoint, "%s%s?status=%s","http://api.twitter.com/1/statuses",
                    "/update.xml", escaped_tweet);
            is_post = 1;
            break;
        case ACTION_MESSAGES:
            if(request->since_id != NULL) {
                sprintf(endpoint, "%s?%s%s", twitter_dm_uri, "since_id=", request->since_id);
            }
            else {
                sprintf(endpoint, "%s", twitter_dm_uri);
            }
            break;

    }
    if(is_post){
        req_url = oauth_sign_url2(endpoint, &postarg, 
                OA_HMAC, NULL, consumer_key, consumer_secret, access_key, access_secret);

        reply = oauth_http_post(req_url, postarg);

    } else{
        req_url = oauth_sign_url2(endpoint, NULL, OA_HMAC, NULL,
                consumer_key, consumer_secret, access_key, access_secret);

        reply = oauth_http_get(req_url, postarg);
    }


    if (request->action != ACTION_UPDATE)
        gsocial_parse(reply, request, request->action);

    if(reply)
        request->exit_code = 1;

    free(postarg);
    free(req_url);
    free(reply);
}
示例#2
0
int API::login(const std::string& email, const std::string& password)
{
    int res = 0;
    std::string url;

    std::string token, secret;

    // Get temporary request token
    url = oauth_sign_url2(this->config.oauth_get_temp_token.c_str(), NULL, OA_HMAC, NULL, CONSUMER_KEY.c_str(), CONSUMER_SECRET.c_str(), NULL /* token */, NULL /* secret */);

    std::string request_token_resp = this->getResponse(url);

    char **rv = NULL;
    int rc = oauth_split_url_parameters(request_token_resp.c_str(), &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if (rc == 3 && !strncmp(rv[1], "oauth_token=", OAUTH_TOKEN_LENGTH) && !strncmp(rv[2], "oauth_token_secret=", OAUTH_SECRET_LENGTH)) {
        token = rv[1]+OAUTH_TOKEN_LENGTH+1;
        secret = rv[2]+OAUTH_SECRET_LENGTH+1;
        rv = NULL;
    }
    else
    {
        return res;
    }

    // Authorize temporary token and get verifier
    url = this->config.oauth_authorize_temp_token + "?username="******"&password="******"oauth_verifier=", OAUTH_VERIFIER_LENGTH)) {
        verifier = rv[1]+OAUTH_VERIFIER_LENGTH+1;
        rv = NULL;
    }
    else
    {
        return res;
    }

    // Get final token and secret
    url = this->config.oauth_get_token + "?oauth_verifier=" + verifier;
    url = oauth_sign_url2(url.c_str(), NULL, OA_HMAC, NULL, CONSUMER_KEY.c_str(), CONSUMER_SECRET.c_str(), token.c_str(), secret.c_str());
    std::string token_resp = this->getResponse(url);

    rc = oauth_split_url_parameters(token_resp.c_str(), &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if (rc == 2 && !strncmp(rv[0], "oauth_token=", OAUTH_TOKEN_LENGTH) && !strncmp(rv[1], "oauth_token_secret=", OAUTH_SECRET_LENGTH)) {
        this->config.oauth_token = rv[0]+OAUTH_TOKEN_LENGTH+1;
        this->config.oauth_secret = rv[1]+OAUTH_SECRET_LENGTH+1;
        free(rv);
        res = 1;
    }

    return res;
}
示例#3
0
// アプリケーション認証にアクセスし、PINコードを取得
int twitter_getpincode( char *tkey_buf , int size )
{
	static const char *request_token_uri = "http://api.twitter.com/oauth/request_token";
	static const char *authorize_uri = "http://api.twitter.com/oauth/authorize";
	static const char *access_token_uri = "http://api.twitter.com/oauth/access_token";

	std::string c_key; //< consumer key
	std::string c_secret; //< consumer secret

	std::string t_key;
	std::string t_secret;

	std::string req_url;
	std::string postarg;
	std::string reply;

	// アプリケーションのキーを代入
	c_key = consumer_key;
	c_secret = consumer_secret;

	// メッセージを表示
	label1->Text="Request token..\n";

    // 認証ページにアクセス
    {
        std::string reply;
        std::string req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, 0, c_key.c_str(), c_secret.c_str(), 0, 0);
        reply = oauth_http_post(req_url.c_str(),postarg.c_str());

		// 反応がなかった場合
		if (!parse_reply(reply.c_str(), &t_key, &t_secret))
        {
			MessageBox (NULL,"failed to get request token.", "ERROR!",MB_OK);
            return FALSE;
        }
    }

	//

	// メッセージを表示
	label1->Text=label1->Text+"Authorize..\n";

	// PINを表示
	{
		std::string req_url = oauth_sign_url2(authorize_uri, 0, OA_HMAC, 0, c_key.c_str(), c_secret.c_str(), t_key.c_str(), t_secret.c_str());

		label1->Text=label1->Text+"Opening...\n";
		puts(req_url.c_str());
		ShellExecute(0, 0, req_url.c_str(), 0, 0, SW_SHOW); // ウェブブラウザを起動する
	}
	return TRUE;
}
示例#4
0
/*
 * Main Test and Example Code.
 * 
 * compile:
 *  gcc -lssl -loauth -o oauthtest oauthtest.c
 */
int main (int argc, char **argv) {
  int fail=0;

  const char *url      = "http://base.url/&just=append?post=or_get_parameters"
                         "&arguments=will_be_formatted_automatically?&dont_care"
			 "=about_separators";
			 //< the url to sign
  const char *c_key    = "1234567890abcdef1234567890abcdef123456789";
  			//< consumer key
  const char *c_secret = "01230123012301230123012301230123";
  			//< consumer secret
  const char *t_key    = "0987654321fedcba0987654321fedcba098765432";
  			//< token key
  const char *t_secret = "66666666666666666666666666666666";
  			//< token secret

#if 1 // example sign GET request and print the signed request URL
  {
    char *geturl = NULL;
    geturl = oauth_sign_url2(url, NULL, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
    printf("GET: URL:%s\n\n", geturl);
    if(geturl) free(geturl);
  }
#endif

#if 1 // sign POST ;) example 
  {
    char *postargs = NULL, *post = NULL;
    post = oauth_sign_url2(url, &postargs, OA_HMAC, NULL, c_key, c_secret, t_key, t_secret);
    printf("POST: URL:%s\n      PARAM:%s\n\n", post, postargs);
    if(post) free(post);
    if(postargs) free(postargs);
  }
#endif

  printf(" *** sending HTTP request *** \n\n");

// These two will perform a HTTP request, requesting an access token. 
// it's intended both as test (verify signature) 
// and example code.
#if 1 // POST a request-token request
  request_token_example_post();
#endif
#if 1 // GET a request-token
  request_token_example_get();
#endif

  return (fail?1:0);
}
示例#5
0
文件: oauth_test.c 项目: wucan/unim
void oauth_test()
{
	// the url to sign
	const char *url = "http://base.url/&just=append?post=or_get_parameters"
					"&arguments=will_be_formatted_automatically?&dont_care"
					"=about_separators";
	// consumer key
	const char *c_key = "1234567890abcdef1234567890abcdef123456789";
	// consumer secret
	const char *c_secret = "01230123012301230123012301230123";
	// token key
	const char *t_key    = "0987654321fedcba0987654321fedcba098765432";
	// token secret
	const char *t_secret = "66666666666666666666666666666666";

	/*
	 * example sign GET request and print the signed request URL
	 */
	{
		char *geturl = NULL;
		geturl = oauth_sign_url2(url, NULL, OA_HMAC, NULL,
					c_key, c_secret, t_key, t_secret);
		printf("GET URL:\n\t%s\n", geturl);
		if (geturl)
			free(geturl);
	}

	/*
	 * sign POST example 
	 */
	{
		char *postargs = NULL, *post;
		post = oauth_sign_url2(url, &postargs, OA_HMAC, NULL,
					c_key, c_secret, t_key, t_secret);
		printf("POST URL:\n\t%s\nPARAM:%s\n", post, postargs);
		if (post)
			free(post);
		if (postargs)
			free(postargs);
	}

	/*
	 * These two will perform a HTTP request, requesting an access token. 
	 * it's intended both as test (verify signature) 
	 */
	request_token_example_get();
	request_token_example_post();
}
示例#6
0
bstring get_oauth_args(char *url, char *method)
{
    char *consumer_key = CONSUMER_KEY;
    char *consumer_secret = CONSUMER_SECRET;
    char *access_token = ACCESS_TOKEN;
    char *access_token_secret = ACCESS_TOKEN_SECRET;

    char *signed_url = NULL;
    char *postarg = NULL;

    signed_url = oauth_sign_url2(
        url, 
        &postarg, 
        OA_HMAC, 
        method, 
        consumer_key, 
        consumer_secret, 
        access_token, 
        access_token_secret);

    free(signed_url);

    bstring bpostarg = bfromcstr(postarg);
    free(postarg);
    return bpostarg; 
}
示例#7
0
gchar* drop_oauth_sign(gchar *consumer_key,
		       gchar *consumer_secret,
		       gchar *access_key,
		       gchar *access_secret,
		       gchar *url,
		       gchar **params,
		       gchar *method)
{
  gchar *requesturl = NULL;
  gchar *resulturl = NULL;

  if(params)
    requesturl = g_strdup_printf("%s?%s", url, *params);
  else
    requesturl = g_strdup(url);

  resulturl = oauth_sign_url2(requesturl,
			      params,
			      OA_HMAC,
			      method,
			      consumer_key,
			      consumer_secret,
			      access_key,
			      access_secret);

  g_free(requesturl);
  return resulturl;
}
示例#8
0
char *gsocial_get_twitter_authorize_url()
{
    int verifier;
    char line[100];
    char *req_url;
    char *reply;
    char ath_uri[90];
    char *new_reply;
    char *twitter_auth_url;

    req_url = oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL,
            consumer_key, consumer_secret, NULL, NULL);
    //printf("%s\n", req_url);

    reply = oauth_http_get(req_url, NULL);
    //printf("%s\n", reply);
    if(gsocial_parse_reply_access(reply, &req_key, &req_secret))
        printf("Something is wrong!\n");

    free(reply);

    //fprintf(stdout, "%s%s\nPIN: ", twitter_authorize_uri, req_key);

    twitter_auth_url = g_strconcat(twitter_authorize_uri, req_key, NULL);
    //g_print(twitter_auth_url);

    return twitter_auth_url;
}
示例#9
0
/************************************Get file(folder) information*****************************************/
string kp_api::GetMetadata(string path)
{		
    path = oauth_url_escape(path.c_str());
	string url = kp_api::m_metadata_url+"/"+kp_api::m_root+path;
	string req_url;
	req_url = oauth_sign_url2(url.c_str(), NULL, OA_HMAC, NULL, m_kpoauth.consumer_key.c_str(),m_kpoauth.consumer_secret.c_str(), m_kpoauth.oauth_token.c_str(),m_kpoauth.oauth_token_secret.c_str());
	string reply = oauth_http_get(req_url.c_str(),NULL);
	return reply;
}
示例#10
0
/************************************Get user account information****************************************/
string kp_api::GetAcountinfo()
{
	//cout<<kp_api::m_kpoauth.oauth_token_secret<<endl;
	string req_url;
	req_url = oauth_sign_url2(kp_api::m_accountinfo_url.c_str(), NULL, OA_HMAC, NULL, m_kpoauth.consumer_key.c_str(),m_kpoauth.consumer_secret.c_str(),
								m_kpoauth.oauth_token.c_str(),m_kpoauth.oauth_token_secret.c_str());
	string reply = oauth_http_get(req_url.c_str(),NULL);
	return reply;
}
示例#11
0
// 取得したキーでつぶやく
int post_tweet(_TCHAR *consumer_key, _TCHAR *consumer_secret, _TCHAR *access_key, _TCHAR *access_secret, _TCHAR *tweet)
{
    std::string c_key; //< consumer key
    std::string c_secret; //< consumer secret

    std::string t_key; //< access key
    std::string t_secret; //< access secret

	std::string message; //< tweetする内容

    std::string req_url;
	std::string postarg;
    std::string reply;

	// アプリケーションのキーを代入
    c_key = consumer_key;
    c_secret = consumer_secret;

    t_key = access_key;
    t_secret = access_secret;

	message = tweet;

	printf("Tweet Message..\n");

	printf("consumer_key: %s\n", c_key.c_str());
	printf("consumer_secret: %s\n", c_secret.c_str());
	printf("access_key: %s\n", t_key.c_str());
	printf("access_secret: %s\n", t_secret.c_str());
	printf("message: %s\n", message.c_str());

	if (!message.empty()) {
		{
			int n;
			wchar_t ucs2[1000];
			char utf8[1000];
			n = MultiByteToWideChar(CP_ACP, 0, message.c_str(), message.size(), ucs2, 1000);
			n = WideCharToMultiByte(CP_UTF8, 0, ucs2, n, utf8, 1000, 0, 0);
			message.assign(utf8, n);
		}
		
		std::string uri = "http://api.twitter.com/statuses/update.json";
		uri += "?status=";
		uri += oauth_url_escape(message.c_str());

		req_url = oauth_sign_url2(uri.c_str(), &postarg, OA_HMAC, 0, c_key.c_str(), c_secret.c_str(), t_key.c_str(), t_secret.c_str());
		reply = oauth_http_post(req_url.c_str(), postarg.c_str());

		/* 取得したjsonをファイルに書き出す */
		FILE *fp;
		fp = fopen("post_message.json","w");
		fputs( reply.c_str() , fp );
		fclose( fp );
	}

    return TRUE;
}
示例#12
0
void
TwitterDialog::tweetCurrentRide()
{

    QString strToken = appsettings->cvalue(context->athlete->cyclist, GC_TWITTER_TOKEN).toString();
    QString strSecret = appsettings->cvalue(context->athlete->cyclist, GC_TWITTER_SECRET).toString();

    QString s_token = QString(strToken);
    QString s_secret = QString(strSecret);

    if(s_token.isEmpty() || s_secret.isEmpty()) {
      #ifdef Q_OS_MACX
      #define GC_PREF tr("Golden Cheetah->Preferences")
      #else
      #define GC_PREF tr("Tools->Options")
      #endif
      QString advise = QString(tr("Error fetching OAuth credentials.  Please make sure to complete the twitter authorization procedure found under %1.")).arg(GC_PREF);
      QMessageBox oautherr(QMessageBox::Critical, tr("OAuth Error"), advise);
      oautherr.exec();
      return;
    }

    char *postarg = NULL;

    // This is for API 1.0
    // QString qurl = "http://api.twitter.com/1/statuses/update.json?status=";
    // This is for API 1.1
    QString qurl = "https://api.twitter.com/1.1/statuses/update.json?status=";

    QString twitterMsg = getTwitterMessage();

    if(twitterMsg.length() > 140) {
      QMessageBox tweetlengtherr(QMessageBox::Critical, tr("Tweet Length Error"), tr("Tweet must be 140 characters or fewer."));
      tweetlengtherr.exec();
      return;
    }

    const QString strUrl = QUrl::toPercentEncoding(twitterMsg);
    qurl.append(strUrl);
    const char *req_url = oauth_sign_url2(qurl.toLatin1(), &postarg, OA_HMAC, NULL, GC_TWITTER_CONSUMER_KEY, GC_TWITTER_CONSUMER_SECRET, s_token.toLatin1(), s_secret.toLatin1());
    const char *strreply = oauth_http_post(req_url,postarg);

    QString post_reply = QString(strreply);

    if(!post_reply.contains("created_at", Qt::CaseInsensitive)) {
      QMessageBox oautherr(QMessageBox::Critical, tr("Error Posting Tweet"), tr("There was an error connecting to Twitter.  Check your network connection and try again."));
      oautherr.setDetailedText(post_reply);
      oautherr.exec();
      return;
    }

    if(postarg) free(postarg);

    accept();

}
示例#13
0
void add_userstream_handle(bear_stream_t *stream,char const* endpoint,bear_stream_handler handler){
	CURL *easy_handle = curl_easy_init();
	char *url;
	url = oauth_sign_url2(endpoint,NULL,OA_HMAC,NULL,stream->c_key,stream->c_sec,stream->t_key,stream->t_sec);
	curl_easy_setopt(easy_handle,CURLOPT_WRITEFUNCTION,curl_write_cb);
	curl_easy_setopt(easy_handle,CURLOPT_WRITEDATA,handler);
	curl_easy_setopt(easy_handle, CURLOPT_URL, url);
	curl_easy_setopt(easy_handle,CURLOPT_SSL_VERIFYPEER,0L);
	curl_multi_add_handle(stream->curl_handle, easy_handle);
}
示例#14
0
/************************************move******************************************/
void kp_api::move(string from_path,string to_path)
{
	char* from_path_encode = oauth_url_escape(from_path.c_str());
	char* to_path_encode = oauth_url_escape(to_path.c_str());
	string url = kp_api::m_move_url+"?root="+kp_api::m_root+"?&from_path="+from_path_encode+"?&to_path="+to_path_encode;
	string req_url = oauth_sign_url2(url.c_str(),NULL,OA_HMAC, NULL, m_kpoauth.consumer_key.c_str(),m_kpoauth.consumer_secret.c_str(), m_kpoauth.oauth_token.c_str	(),m_kpoauth.oauth_token_secret.c_str());
	//cout<<req_url<<endl;
	string reply = oauth_http_get(req_url.c_str(),NULL);
	cout<<reply<<endl;
}
示例#15
0
std::string API::getResponseOAuth(const std::string& url)
{
    #ifdef DEBUG
        std::cerr << "DEBUG INFO (API::getResponseOAuth)" << std::endl << "URL: " << url << std::endl;
    #endif
    std::string url_oauth = oauth_sign_url2(url.c_str(), NULL, OA_HMAC, NULL, CONSUMER_KEY.c_str(), CONSUMER_SECRET.c_str(), this->config.oauth_token.c_str(), this->config.oauth_secret.c_str());
    std::string response = this->getResponse(url_oauth);

    return response;
}
示例#16
0
/************************************delete function:*****************************************/
string kp_api::Delete(string path)
{
	string path_encode = oauth_url_escape(path.c_str());	
	string url = kp_api::m_delete_url+"?root="+kp_api::m_root+"?&path="+path_encode;
    string req_url =
        oauth_sign_url2(url.c_str(), NULL, OA_HMAC, NULL, m_kpoauth.consumer_key.c_str(),m_kpoauth.consumer_secret.c_str(), m_kpoauth.oauth_token.c_str	(),m_kpoauth.oauth_token_secret.c_str());
	//cout<<req_url<<endl;
	string reply = oauth_http_get(req_url.c_str(),NULL);
	return reply;
}
示例#17
0
// PINを使ってアクセスキーとアクセスシークレットを取得
int twitter_setpincode( char *tkey_buf , int code )
{
	
		static const char *access_token_uri = "http://api.twitter.com/oauth/access_token";

		std::string c_key; //< consumer key
		std::string c_secret; //< consumer secret
		
		std::string t_key;
		std::string t_secret;
		
		std::string req_url;
		std::string postarg;
		std::string reply;
		
		//コンシューマーキーとコンシューマーシークレットを代入
		c_key = consumer_key;
		c_secret = consumer_secret;

		// アクセストークンを取得し、t_key,t_secretに格納
		{
			char buffer[256];
			label1->Text=label1->Text+"Access token..\n";
			
			std::string url = access_token_uri;
			url += "?oauth_verifier=";
			url +=  itoa( code , buffer , 10 );

			std::string req_url = oauth_sign_url2(url.c_str(), 0, OA_HMAC, 0, c_key.c_str(), 0, t_key.c_str(), 0);
			std::string reply = oauth_http_get(req_url.c_str(), postarg.c_str());

			// 反応がなかった場合
			if (!parse_reply(reply.c_str(), &t_key, &t_secret))
       		{

				MessageBox (NULL,"failed to get request token.", "ERROR!",MB_OK);
        		return FALSE;
        	}
		}

		 // 't_key' と 't_secret' を表示
		label1->Text=label1->Text+"access key: "+ t_key.c_str()+"\n";
		label1->Text=label1->Text+"access secret: "+ t_secret.c_str()+"\n";

		//とりあえずファイルに書き出す
		{
        	FILE *fp;
        	fp = fopen("access.txt","a");
        	fprintf( fp , "%s\r\n%s\r\n" , t_key.c_str() , t_secret.c_str() );
        	fclose( fp );
   		}
   		return TRUE;
}
示例#18
0
/**
 * Free after use
 */
gchar* osm_oauth_sign_url ( const gchar* url, const char *method )
{
	gchar *signed_url = NULL;
	const gchar *access_token_key_pref = a_preferences_get(OSM_ACCESS_TOKEN_KEY)->s;
	const gchar *access_token_secret_pref = a_preferences_get(OSM_ACCESS_TOKEN_SECRET)->s;

	if ( access_token_key_pref && access_token_secret_pref &&
	     strlen(access_token_key_pref) > 1 && strlen(access_token_secret_pref) > 1 ) {
		signed_url = oauth_sign_url2 ( url, NULL, OA_HMAC, method, viking_consumer_key, viking_consumer_secret, access_token_key_pref, access_token_secret_pref );
	}
	return signed_url;
}
// Construct the strings for the http.
static int ConstructStringsForMethod(SoundCloudCAPI *api,const char* httpMethod,const char *resource,SoundCloudCAPI_Parameter* parameters,int num_params,char **url,char **body,size_t* bodylen,char **header)
{
	char *newurl,*suffix,*tail,*boundary,*headerstring,*pstring,*postargs=0,*sigurl,*signed_url;
	size_t datalen,headlen,taillen,urllen;

	if (api->t_type!=2)	return SoundCloudCAPICallback_didFailWithError;
	
	// Construct the root of the url.
	headlen=strlen(api->apiBaseURL)+strlen(resource);
	if (*resource!='/') headlen++;
	taillen=strlen(httpMethod)+10+4;
	urllen=headlen+taillen;
	newurl=(char*)malloc(urllen+1);newurl[urllen]=0;
	
	// Here's the URL and the resource.
	sprintf(newurl,"%s%s%s",api->apiBaseURL,(*resource=='/')?"":"/",resource);
	if (newurl[headlen-1]=='/') headlen--;
	suffix=newurl+headlen;
	// Add the suffix to request the response format
	tail=suffix+sprintf(suffix,".%s",(api->responseFormat)?"js":"xml");
	// Append the access method
//	tail+=sprintf(tail,"?_method=%s",httpMethod);
	// Unsigned URL is complete.
	
	// Generate a boundary and header for any multipart we have.
	boundary=GenerateBoundary();
	headerstring=(char*)malloc(100+strlen(boundary));	// construct a header string
	sprintf(headerstring,"Content-Type: multipart/form-data; boundary=%s",boundary);
	
	// Construct a binary field containing all the parameter data (files, strings etc).
	pstring=ParametersToString(parameters, num_params,&datalen,boundary);
	if (!datalen) {free(headerstring);headerstring=(char*)malloc(1);*headerstring=0;}	// don't use a multipart header if we don't have actual data.

	// Sign the URL.
	sc_log(api,SoundCloudCAPI_LogLevel_Debug,"Signing url [%s], method [%s], with [%s][%s][%s][%s]\n",newurl,httpMethod,api->consumerKey,api->consumerSecret,api->t_key,api->t_secret);
	sigurl = oauth_sign_url2(newurl, &postargs, OA_HMAC, httpMethod, api->consumerKey, api->consumerSecret, api->t_key, api->t_secret);
	
	// Concatenate the post args onto the url.
	signed_url=(char*)malloc(2+strlen(sigurl)+strlen(postargs));sprintf(signed_url,"%s?%s",sigurl,postargs);
	
	sc_log(api,SoundCloudCAPI_LogLevel_Debug,"Sending Url [%s], header[%s], body[%s]\n",signed_url,headerstring,pstring);
	
	free(newurl);free(boundary);free(sigurl);if(postargs)free(postargs);
	
	(*header)=headerstring;	// SAVE
	(*body)=pstring;
	(*bodylen)=datalen;
	(*url)=signed_url;
	return 0;
}
示例#20
0
int main(int argc, const char* argv[])
{	

	if(!bcm2835_init())
	{
		printf("%s\n", "BCM2835 failed to initialize");
		return 0;
	}

	bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);

	count = 0;
	
	if(argc != 2)
	{
		printf("usage: startbrewing file\n");
		return 0; 
	} else {
		const char *url = "https://userstream.twitter.com/1.1/user.json";
		oauth_keys *keys = malloc(sizeof(oauth_keys)+1);	

		keys->ckey = malloc(50);
		keys->csecret = malloc(50);
		keys->atok = malloc(50);
		keys->atoksecret = malloc(50);
		
		char *filename = malloc(strlen(argv[1])+1);
	
		strcpy(filename, argv[1]);
		
		read_in_keys(filename, keys);
		
		curl_global_init(CURL_GLOBAL_ALL);
		
		CURL *curl = curl_easy_init();
		char *signedurl = oauth_sign_url2(url, NULL, OA_HMAC, "GET", keys->ckey, keys->csecret, keys->atok, keys->atoksecret);
		
		curl_easy_setopt(curl, CURLOPT_URL, signedurl);
		curl_easy_setopt(curl, CURLOPT_USERAGENT, "Start Brewing/0.1");
		curl_easy_setopt(curl, CURLOPT_FAILONERROR, 1);
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, parse);

		int curl_status = curl_easy_perform(curl);
		
		curl_easy_cleanup(curl);
		curl_global_cleanup();
	}
	
	return 0;
}
示例#21
0
文件: utils.c 项目: trorornmn/ctumblr
void http_request(const char *url, enum http_request_type type, char **rep)
{
	char *post = NULL, *request = NULL;
	CURL *curl;
	CURLcode ret;

	curl = curl_easy_init();
	if (curl == NULL) {
		fprintf(stderr, "curl_easy_init() failed.\n");
		exit(EXIT_FAILURE);
	}

	if (type == GET) {
		request = oauth_sign_url2(url, NULL, OA_HMAC, NULL, user_oatuh_keys.c_key, user_oatuh_keys.c_sec, user_oatuh_keys.t_key, user_oatuh_keys.t_sec);
	} else if (type == POST) {
		request = oauth_sign_url2(url, &post, OA_HMAC, NULL, user_oatuh_keys.c_key, user_oatuh_keys.c_sec, user_oatuh_keys.t_key, user_oatuh_keys.t_sec);

		curl_easy_setopt (curl, CURLOPT_POSTFIELDS, (void *)post);
	}

	curl_easy_setopt(curl, CURLOPT_URL, request);
	curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);

	curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)(rep));
	curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

	ret = curl_easy_perform(curl);
	if (ret != CURLE_OK) {
		fprintf(stderr, "curl_easy_perform() failed.\n");
		exit(EXIT_FAILURE);
	}

	free_and_assign(request);
	free_and_assign(post);

	curl_easy_cleanup(curl);
}
示例#22
0
文件: oauth_test.c 项目: wucan/unim
/* 
 * a example requesting and parsing a request-token from an OAuth
 * service-provider, excercising the oauth-HTTP GET function.
 * it is almost the same as \ref request_token_example_post below. 
 */
void request_token_example_get()
{
	const char *request_token_uri =
			"http://term.ie/oauth/example/request_token.php";
	const char *req_c_key = "key";
	const char *req_c_secret = "secret";
	char *res_t_key = NULL; // replied key
	char *res_t_secret = NULL; // replied secret
	char *req_url;
	char *reply;

	printf("***** request token example, use method get *****\n");
	req_url = oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL,
					req_c_key, req_c_secret, NULL, NULL);
	printf("request url:\n\t%s -> \n\t%s\n", request_token_uri, req_url);
	reply = oauth_http_get(req_url, NULL);
	if (!reply) {
		printf("HTTP request for an oauth request-token failed!\n");
	} else {
		/*
		 * parse reply - example:
		 * "oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
		 */
		int rc;
		char **rv = NULL;

		printf("HTTP-reply:\n\t%s\n", reply);
		rc = oauth_split_url_parameters(reply, &rv);
		qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
		if (rc == 2 &&
			!strncmp(rv[0], "oauth_token=", 11) &&
			!strncmp(rv[1], "oauth_token_secret=", 18)) {
			res_t_key = strdup(&(rv[0][12]));
			res_t_secret = strdup(&(rv[1][19]));
			printf("key:    '%s'\nsecret: '%s'\n", res_t_key, res_t_secret);
		}
		if (rv)
			free(rv);
	}

	if (req_url)
		free(req_url);
	if (reply)
		free(reply);
	if (res_t_key)
		free(res_t_key);
	if (res_t_secret)
		free(res_t_secret);
}
示例#23
0
文件: oauth_test.c 项目: wucan/unim
/*
 * a example requesting and parsing a request-token from an OAuth
 * service-provider, using the oauth-HTTP POST function.
 */
void request_token_example_post()
{
	const char *request_token_uri =
				"http://term.ie/oauth/example/request_token.php";
	const char *req_c_key = "key";
	const char *req_c_secret = "secret";
	char *res_t_key = NULL;
	char *res_t_secret = NULL;
	char *postarg = NULL;
	char *req_url;
	char *reply;

	printf("***** request token example, use method post *****\n");
	req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL,
						req_c_key, req_c_secret, NULL, NULL);
	printf("request url:\n\t%s -> \n\t%s\n", request_token_uri, req_url);
	reply = oauth_http_post(req_url, postarg);
	printf("post arg:\n\t%s\n", postarg);
	if (!reply)  {
		printf("HTTP request for an oauth request-token failed!\n");
	} else {
		int rc;
		char **rv = NULL;
		printf("HTTP-reply:\n\t%s\n", reply);
		rc = oauth_split_url_parameters(reply, &rv);
		qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
		if (rc == 2 &&
			!strncmp(rv[0], "oauth_token=", 11) &&
			!strncmp(rv[1], "oauth_token_secret=", 18)) {
			res_t_key = strdup(&(rv[0][12]));
			res_t_secret = strdup(&(rv[1][19]));
			printf("key:    '%s'\nsecret: '%s'\n", res_t_key, res_t_secret);
		}
		if (rv)
			free(rv);
	}

	if (req_url)
		free(req_url);
	if (postarg)
		free(postarg);
	if (reply)
		free(reply);
	if (res_t_key)
		free(res_t_key);
	if (res_t_secret)
		free(res_t_secret);
}
示例#24
0
bool kp_api::Upload(string upload_path,string local_path)
{
	char *postarg = NULL;
	string path = oauth_url_escape(upload_path.c_str());
	string upload_url = kp_api::GetUploadUrl()+"1/fileops/upload_file?overwrite=true?&root="+kp_api::m_root+"?&path="+path;
	string req_url = oauth_sign_url2(upload_url.c_str(),&postarg,OA_HMAC,NULL,m_kpoauth.consumer_key.c_str(),m_kpoauth.consumer_secret.c_str(),m_kpoauth.oauth_token.c_str(),m_kpoauth.oauth_token_secret.c_str());
/*Parameters:
url 	The request URL to be signed. append all GET or POST query-parameters separated by either '?' or '&' to this parameter.
postargs 	This parameter points to an area where the return value is stored. If 'postargs' is NULL, no value is stored.
method 	specify the signature method to use. It is of type OAuthMethod and most likely OA_HMAC.
http_method 	The HTTP request method to use (ie "GET", "PUT",..) If NULL is given as 'http_method' this defaults to "GET" when 'postargs' is also NULL and when postargs is not NULL "POST" is used.
*/
//回来在这里写liburl上传文件的程序...
	//cout<<req_url<<endl;
	req_url = req_url+"?"+postarg;
	//cout<<req_url<<endl;
//upload file using the http post way of curl
	CURL *curl;
	CURLcode res;
	struct curl_httppost *formpost=NULL;
	struct curl_httppost *lastptr=NULL;
	struct curl_slist *headerlist=NULL;
	static const char buf[]="Expect:";
	
	curl_global_init(CURL_GLOBAL_ALL);
	
	curl_formadd(&formpost,
				&lastptr,
				CURLFORM_COPYNAME,"file",
				CURLFORM_FILE,local_path.c_str(),
				CURLFORM_END);

	curl = curl_easy_init();
	headerlist = curl_slist_append(headerlist,buf);
	curl_easy_setopt(curl,CURLOPT_URL,req_url.c_str());
	curl_easy_setopt(curl,CURLOPT_HTTPHEADER,headerlist);	
	curl_easy_setopt(curl,CURLOPT_HTTPPOST,formpost);
	//curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
	//curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
	res = curl_easy_perform(curl);
	int infocode;
	curl_easy_getinfo(curl,CURLINFO_RESPONSE_CODE,&infocode);
	
	curl_easy_cleanup(curl);
	curl_formfree(formpost);	
	curl_slist_free_all(headerlist);
	return infocode==200 ? true:false;
}
示例#25
0
CURLcode submit_weibo_with_url_image1(char* szContent, char* szImageUrl)
{
    POS();
    char *postargs = NULL;
    char* post = NULL;
    post = oauth_sign_url2("http://api.t.sina.com.cn/statuses/update.json?status=teeeest1314353455", &postargs, OA_HMAC, NULL, "3551174478", "c53f4123bdb7b39a0ae03e400b839934", "18d2d221a6bc0aa8743d3f30d6745b7e", "2de591d207b213e4f46a1d1170413367");

    DEBUG("%s\n", post);
    DEBUG("%s\n", postargs);
    char *reply = oauth_http_post(post,postargs);
    if(reply){
        DEBUG("%s\n", reply);
    }

    return 0;
}
示例#26
0
/*
 * a example requesting and parsing a request-token from an OAuth service-provider
 * using the oauth-HTTP POST function.
 */
void request_token_example_post(void) {
#if 0
  const char *request_token_uri = "http://oauth-sandbox.mediamatic.nl/module/OAuth/request_token";
  const char *req_c_key         = "17b09ea4c9a4121145936f0d7d8daa28047583796"; //< consumer key
  const char *req_c_secret      = "942295b08ffce77b399419ee96ac65be"; //< consumer secret
#else
  const char *request_token_uri = "http://term.ie/oauth/example/request_token.php";
  const char *req_c_key         = "key"; //< consumer key
  const char *req_c_secret      = "secret"; //< consumer secret
#endif
  char *res_t_key    = NULL; //< replied key
  char *res_t_secret = NULL; //< replied secret

  char *postarg = NULL;
  char *req_url;
  char *reply;

  req_url = oauth_sign_url2(request_token_uri, &postarg, OA_HMAC, NULL, req_c_key, req_c_secret, NULL, NULL);

  printf("request URL:%s\n\n", req_url);
  reply = oauth_http_post(req_url,postarg);
  if (!reply) 
    printf("HTTP request for an oauth request-token failed.\n");
  else {
    //parse reply - example:
    //"oauth_token=2a71d1c73d2771b00f13ca0acb9836a10477d3c56&oauth_token_secret=a1b5c00c1f3e23fb314a0aa22e990266"
    int rc;
    char **rv = NULL;
    printf("HTTP-reply: %s\n", reply);
    rc = oauth_split_url_parameters(reply, &rv);
    qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
    if( rc==2 
	&& !strncmp(rv[0],"oauth_token=",11)
	&& !strncmp(rv[1],"oauth_token_secret=",18) ){
	  res_t_key=strdup(&(rv[0][12]));
	  res_t_secret=strdup(&(rv[1][19]));
	  printf("key:    '%s'\nsecret: '%s'\n",res_t_key, res_t_secret);
    }
    if(rv) free(rv);
  }

  if(req_url) free(req_url);
  if(postarg) free(postarg);
  if(reply) free(reply);
  if(res_t_key) free(res_t_key);
  if(res_t_secret) free(res_t_secret);
}
示例#27
0
void drop_oauth_access_token(gchar *consumer_key,
			     gchar *consumer_secret,
			     gchar *request_key,
			     gchar *request_secret,
			     gchar **access_key,
			     gchar **access_secret,
			     GError **error)
{
  gchar *response = NULL;
  gint argc = 0;
  gchar **argv = NULL;
  gchar *postarg = NULL;
  gchar **token = NULL;

  oauth_sign_url2(ACCESS_TOKEN_URL,
		  &postarg,
		  OA_HMAC,
		  NULL,
		  consumer_key,
		  consumer_secret,
		  request_key,
		  request_secret);

  response = drop_soup_sync(ACCESS_TOKEN_URL, postarg, DROP_SOUP_OAUTH, error);
  argc = oauth_split_post_paramters(response, &argv, 0);

  while(argc)
    {
      token = g_strsplit(argv[argc - 1], "=", 0);
      if(g_strcmp0(token[0], "oauth_token") == 0)
	*access_key = g_strdup(token[1]);
      if(g_strcmp0(token[0], "oauth_token_secret") == 0)
	*access_secret = g_strdup(token[1]);
      argc--;
    }

  if(access_key == NULL || access_secret == NULL)
    {
      g_print("things went wrong!! not able to get access_{key,secret}!!\n");
      exit(EXIT_FAILURE);
    }

  g_free(postarg);
  g_free(response);
  g_strfreev(token);
  oauth_free_array(&argc, &argv);
}
示例#28
0
int main(int argc, char **argv) {
	if (argc != 6) {
		fprintf(stderr, "Usage: %s URL consumerkey consumersecret token tokensecret\n\n", argv[0]);
		exit(EXIT_FAILURE);
	}

	char *url = argv[1];
	char *c_key = argv[2];
	char *c_secret = argv[3];
	char *t_key = argv[4];
	char *t_secret = argv[5];

	char *sig = oauth_sign_url2(url, NULL, OA_HMAC, "GET", c_key, c_secret, t_key, t_secret);
	char *separator = "";

	printf("Authorization: OAuth ");

	// Can't just use oauth_split_url_parameters() because it loses the last one (?!?)

	char *s;
	for (s = sig; *s; s++) {
		if (*s == '?' || *s == '&') {
			if (strncmp(s + 1, "oauth_", 6) == 0) {
				printf("%s", separator);
				separator = ", ";

				char *cp;
				for (cp = s + 1; *cp && *cp != '&'; cp++) {
					putchar(*cp);
					if (*cp == '=') {
						cp++;
						break;
					}
				}
				putchar('"');
				for (; *cp && *cp != '&'; cp++) {
					putchar(*cp);
				}
				putchar('"');
			}
		}
	}

	putchar('\n');
	return 0;
}
示例#29
0
int twitter_user_timeline( char *lastid , _TCHAR *consumer_key, _TCHAR *consumer_secret, _TCHAR *access_key, _TCHAR *access_secret)
{
    std::string c_key; //< consumer key
    std::string c_secret; //< consumer secret

    std::string t_key; //< access key
    std::string t_secret; //< access secret

    std::string req_url;
    std::string reply;

    // アプリケーションのキーを代入
    c_key = consumer_key;
    c_secret = consumer_secret;

    t_key = access_key;
    t_secret = access_secret;

    printf("Request user timeline..\n");

    printf("consumer_key: %s\n", c_key.c_str());
    printf("consumer_secret: %s\n", c_secret.c_str());
    printf("access_key: %s\n", t_key.c_str());
    printf("access_secret: %s\n", t_secret.c_str());

    std::string uri = "http://api.twitter.com/1/statuses/user_timeline.json";

    /* lastid以降を取得する場合 */
    //uri += "?since_id=";
    //uri += lastid;

    /* 最新の○件の場合 */
    uri += "?count=";
    uri += "100";

    req_url = oauth_sign_url2(uri.c_str(), 0, OA_HMAC, 0, c_key.c_str(), c_secret.c_str(), t_key.c_str(), t_secret.c_str());
    reply = oauth_http_get(req_url.c_str(), NULL );

    /* 取得したxmlをファイルに書き出す */
    FILE *fp;
    fp = fopen("user_timeline.json","w");
    fputs( reply.c_str() , fp );
    fclose( fp );

    return TRUE;
}
示例#30
0
char *gsocial_get_access_key_full_reply(char *pin)
{
    char *req_url;
    char ath_uri[90];
    char *new_reply;


    sprintf(ath_uri, "%s?oauth_verifier=%s", access_token, pin);

    req_url = oauth_sign_url2(ath_uri, NULL, OA_HMAC, NULL, consumer_key,
            consumer_secret, req_key, req_secret);

    new_reply = oauth_http_get(req_url, NULL);

    free(req_key);
    free(req_secret);

    return new_reply;
}