#5
by DAICHI0922 » 6年前
ありがとうございます。
Twitcurlはマルチスレッドデバッグに対応していないことがわかり、修正しました。
コードは変わってしまうのですが、Twitterのアクセストークンをユーザー名とパスワードから出すにはどのようにしたらいいのでしょうか。ネットのサンプルとTwitcurl公式の説明を繋ぎ合わして以下のコードを書いたのですが、{"errors":[{"code":89,"message":"Invalid or expired token."}]}という応答が帰ってきます。つまりアクセストークンが上手く取得できていないということです。
どこがおかしいかご教授いただけると幸いです。
コード:
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "twitcurl.h"
#pragma comment(lib,"legacy_stdio_definitions.lib")
using namespace std;
int main(){
string tweet;
cout << "Tweet > "; cin >> tweet;
string userName("UsersName"); //ユーザ名
string passWord("PassWord"); //パスワード
string myConsumerKey("0000");
string myConsumerSecuret("0000");
//string myOAuthAccessTokenKey("0000");
//string myOAuthAccessTokenSecret("0000");
string myOAuthAccessTokenKey;
string myOAuthAccessTokenSecret;
ifstream oAuthTokenKeyIn;
ifstream oAuthTokenSecretIn;
twitCurl twitterObj;
string tmpStr;
string replyMsg;
char tmpBuf[1024];
/* Set twitter username and password */
twitterObj.setTwitterUsername(userName);
twitterObj.setTwitterPassword(passWord);
/* OAuth flow begins */
twitterObj.getOAuth().setConsumerKey(myConsumerKey);
twitterObj.getOAuth().setConsumerSecret(myConsumerSecuret);
/* Step 1: Check if we alredy have OAuth access token from a previous run */
oAuthTokenKeyIn.open("twitterClient_token_key.txt");
oAuthTokenSecretIn.open("twitterClient_token_secret.txt");
memset(tmpBuf, 0, 1024);
oAuthTokenKeyIn >> tmpBuf;
myOAuthAccessTokenKey = tmpBuf;
memset(tmpBuf, 0, 1024);
oAuthTokenSecretIn >> tmpBuf;
myOAuthAccessTokenSecret = tmpBuf;
oAuthTokenKeyIn.close();
oAuthTokenSecretIn.close();
if (myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size()){
/* If we already have these keys, then no need to go through auth again */
printf("\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str());
twitterObj.getOAuth().setOAuthTokenKey(myOAuthAccessTokenKey);
twitterObj.getOAuth().setOAuthTokenSecret(myOAuthAccessTokenSecret);
}
else{
/* Step 2: Get request token key and secret */
string authUrl;
twitterObj.oAuthRequestToken(authUrl);
/* Step 3: Get PIN */
/* pass auth url to twitCurl and get it via twitCurl PIN handling */
twitterObj.oAuthHandlePIN(authUrl);
/* Step 4: Exchange request token with access token */
twitterObj.oAuthAccessToken();
/* Step 5: save this access token key and secret for future use */
twitterObj.getOAuth().getOAuthTokenKey(myOAuthAccessTokenKey);
twitterObj.getOAuth().getOAuthTokenSecret(myOAuthAccessTokenSecret);
/* Step 6: Save these keys in a file or wherever */
ofstream oAuthTokenKeyOut;
ofstream oAuthTokenSecretOut;
oAuthTokenKeyOut.open("twitterClient_token_key.txt");
oAuthTokenSecretOut.open("twitterClient_token_secret.txt");
oAuthTokenKeyOut.clear();
oAuthTokenSecretOut.clear();
oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str();
oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str();
oAuthTokenKeyOut.close();
oAuthTokenSecretOut.close();
}
/* OAuth flow ends */
/* Account credentials verification */
if (twitterObj.accountVerifyCredGet()){
twitterObj.getLastWebResponse(replyMsg);
printf("\naccountVerifyCredGet web response:\n%s\n", replyMsg.c_str());
}
else{
twitterObj.getLastCurlError(replyMsg);
printf("\naccountVerifyCredGet error:\n%s\n", replyMsg.c_str());
}
/* Post a new status message */
tmpStr = tweet;
replyMsg = "";
if (twitterObj.statusUpdate(tmpStr)){
twitterObj.getLastWebResponse(replyMsg);
printf("\nstatusUpdate web response:\n%s\n", replyMsg.c_str());
}
else{
twitterObj.getLastCurlError(replyMsg);
printf("\nstatusUpdate error:\n%s\n", replyMsg.c_str());
}
return 0;
}
ありがとうございます。
Twitcurlはマルチスレッドデバッグに対応していないことがわかり、修正しました。
コードは変わってしまうのですが、Twitterのアクセストークンをユーザー名とパスワードから出すにはどのようにしたらいいのでしょうか。ネットのサンプルとTwitcurl公式の説明を繋ぎ合わして以下のコードを書いたのですが、{"errors":[{"code":89,"message":"Invalid or expired token."}]}という応答が帰ってきます。つまりアクセストークンが上手く取得できていないということです。
どこがおかしいかご教授いただけると幸いです。
[code]
#include <stdio.h>
#include <iostream>
#include <fstream>
#include "twitcurl.h"
#pragma comment(lib,"legacy_stdio_definitions.lib")
using namespace std;
int main(){
string tweet;
cout << "Tweet > "; cin >> tweet;
string userName("UsersName"); //ユーザ名
string passWord("PassWord"); //パスワード
string myConsumerKey("0000");
string myConsumerSecuret("0000");
//string myOAuthAccessTokenKey("0000");
//string myOAuthAccessTokenSecret("0000");
string myOAuthAccessTokenKey;
string myOAuthAccessTokenSecret;
ifstream oAuthTokenKeyIn;
ifstream oAuthTokenSecretIn;
twitCurl twitterObj;
string tmpStr;
string replyMsg;
char tmpBuf[1024];
/* Set twitter username and password */
twitterObj.setTwitterUsername(userName);
twitterObj.setTwitterPassword(passWord);
/* OAuth flow begins */
twitterObj.getOAuth().setConsumerKey(myConsumerKey);
twitterObj.getOAuth().setConsumerSecret(myConsumerSecuret);
/* Step 1: Check if we alredy have OAuth access token from a previous run */
oAuthTokenKeyIn.open("twitterClient_token_key.txt");
oAuthTokenSecretIn.open("twitterClient_token_secret.txt");
memset(tmpBuf, 0, 1024);
oAuthTokenKeyIn >> tmpBuf;
myOAuthAccessTokenKey = tmpBuf;
memset(tmpBuf, 0, 1024);
oAuthTokenSecretIn >> tmpBuf;
myOAuthAccessTokenSecret = tmpBuf;
oAuthTokenKeyIn.close();
oAuthTokenSecretIn.close();
if (myOAuthAccessTokenKey.size() && myOAuthAccessTokenSecret.size()){
/* If we already have these keys, then no need to go through auth again */
printf("\nUsing:\nKey: %s\nSecret: %s\n\n", myOAuthAccessTokenKey.c_str(), myOAuthAccessTokenSecret.c_str());
twitterObj.getOAuth().setOAuthTokenKey(myOAuthAccessTokenKey);
twitterObj.getOAuth().setOAuthTokenSecret(myOAuthAccessTokenSecret);
}
else{
/* Step 2: Get request token key and secret */
string authUrl;
twitterObj.oAuthRequestToken(authUrl);
/* Step 3: Get PIN */
/* pass auth url to twitCurl and get it via twitCurl PIN handling */
twitterObj.oAuthHandlePIN(authUrl);
/* Step 4: Exchange request token with access token */
twitterObj.oAuthAccessToken();
/* Step 5: save this access token key and secret for future use */
twitterObj.getOAuth().getOAuthTokenKey(myOAuthAccessTokenKey);
twitterObj.getOAuth().getOAuthTokenSecret(myOAuthAccessTokenSecret);
/* Step 6: Save these keys in a file or wherever */
ofstream oAuthTokenKeyOut;
ofstream oAuthTokenSecretOut;
oAuthTokenKeyOut.open("twitterClient_token_key.txt");
oAuthTokenSecretOut.open("twitterClient_token_secret.txt");
oAuthTokenKeyOut.clear();
oAuthTokenSecretOut.clear();
oAuthTokenKeyOut << myOAuthAccessTokenKey.c_str();
oAuthTokenSecretOut << myOAuthAccessTokenSecret.c_str();
oAuthTokenKeyOut.close();
oAuthTokenSecretOut.close();
}
/* OAuth flow ends */
/* Account credentials verification */
if (twitterObj.accountVerifyCredGet()){
twitterObj.getLastWebResponse(replyMsg);
printf("\naccountVerifyCredGet web response:\n%s\n", replyMsg.c_str());
}
else{
twitterObj.getLastCurlError(replyMsg);
printf("\naccountVerifyCredGet error:\n%s\n", replyMsg.c_str());
}
/* Post a new status message */
tmpStr = tweet;
replyMsg = "";
if (twitterObj.statusUpdate(tmpStr)){
twitterObj.getLastWebResponse(replyMsg);
printf("\nstatusUpdate web response:\n%s\n", replyMsg.c_str());
}
else{
twitterObj.getLastCurlError(replyMsg);
printf("\nstatusUpdate error:\n%s\n", replyMsg.c_str());
}
return 0;
}
[/code]