Added error checking to Twitter OAuth

.. but cannot test, so please check it !
This commit is contained in:
Mark Liversedge
2014-07-14 12:07:22 +01:00
parent 8e64e4f8db
commit cc4f2b5d10
2 changed files with 59 additions and 27 deletions

View File

@@ -41,39 +41,56 @@ OAuthDialog::OAuthDialog(Context *context, OAuthSite site) :
urlstr.append("redirect_uri=http://www.goldencheetah.org/&");
urlstr.append("response_type=code&");
urlstr.append("approval_prompt=force");
}
else if (site == TWITTER) {
} else if (site == TWITTER) {
#ifdef GC_HAVE_LIBOAUTH
int rc;
char **rv = NULL;
QString token;
QString url = QString();
t_key = NULL;
t_secret = NULL;
const char *request_token_uri = "https://api.twitter.com/oauth/request_token";
char *req_url = NULL;
char *postarg = NULL;
char *reply = NULL;
// get the url
req_url = oauth_sign_url2(request_token_uri, NULL, OA_HMAC, NULL, GC_TWITTER_CONSUMER_KEY, GC_TWITTER_CONSUMER_SECRET, NULL, NULL);
reply = oauth_http_get(req_url,postarg);
if (req_url != NULL) {
rc = oauth_split_url_parameters(reply, &rv);
qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
token = QString(rv[1]);
t_key =strdup(&(rv[1][12]));
t_secret =strdup(&(rv[2][19]));
urlstr = QString("https://api.twitter.com/oauth/authorize?");
urlstr.append(token);
//QDesktopServices::openUrl(QUrl(url));
if(rv) free(rv);
// post it
reply = oauth_http_get(req_url,postarg);
if (reply != NULL) {
//urlstr.append("&oauth_callback=http%3A%2F%2Fwww.goldencheetah.org%2F");
requestToken = true;
// will split reply into paramters using strdup
rc = oauth_split_url_parameters(reply, &rv);
if (rc >= 3) {
// really ?
qsort(rv, rc, sizeof(char *), oauth_cmpstringp);
token = QString(rv[1]);
t_key =strdup(&(rv[1][12]));
t_secret =strdup(&(rv[2][19]));
urlstr = QString("https://api.twitter.com/oauth/authorize?");
urlstr.append(token);
// free memory using count rc
for(int i=0; i<rc; i++) free(rv[i]);
}
//urlstr.append("&oauth_callback=http%3A%2F%2Fwww.goldencheetah.org%2F");
requestToken = true;
}
}
#endif
}
else if (site == CYCLING_ANALYTICS) {
} else if (site == CYCLING_ANALYTICS) {
urlstr = QString("https://www.cyclinganalytics.com/api/auth?");
urlstr.append("client_id=").append(GC_CYCLINGANALYTICS_CLIENT_ID).append("&");
urlstr.append("scope=modify_rides&");

View File

@@ -130,22 +130,37 @@ TwitterDialog::tweetCurrentRide()
return;
}
bool failed = true;
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);
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());
char *strreply;
QString post_reply;
QString post_reply = QString(strreply);
if (req_url != NULL) {
strreply = oauth_http_post(req_url,postarg);
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 (strreply != NULL) {
post_reply = QString(strreply);
if (post_reply.contains("created_at", Qt::CaseInsensitive)) {
failed = false;
}
}
if (postarg) free(postarg);
}
if(postarg) free(postarg);
// let user know it didn't work
if (failed) {
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); // probably blank
oautherr.exec();
return;
}
// otherwise all done
accept();
}