Hello Steve/Anton,
I am trying code below - but i get UnAuthorized error (or) / 404 Page Not found error - based on what i enter as URL.
In code below when i use
URL url = new URL("
https://MyLogin.freshbooks.com/api/2.1/xml-in -d '<request method=\"invoice.list\" />'");
(I get 404 page not found)
URL url = new URL("
https://MyLogin.freshbooks.com/api/2.1/xml-in");
(Response: 401 Unauthorized) I guess i am messing up with tokens to be passed and syntax and encoding - once i have obtained them from freshbooks.
I am not sure i am in right direction as what i want to do is below
1) develop a java code which will accept input as customerfreshbookkey (his
mylogin.
freshbooks.com) and also his oAuthSecret key (which he will get after registering for oAuthAPI().
2) With above i want to get data out of freshbooks for this customer from invoice.list, staff.list and others similar list in java, which will be used for reporting.
3) I guess for step#2 i have to get customers oAuthSecret key - and they have to request for freshbooks API - or is it possible that customer can get their using java from freshbooks api without even registering for freshbook API()? Let me know your thoughts.
I plan to do it as below - to which i could proceed to some extent (with blessings from you and Anton) but i am still not able to do what i require.
So again requesting your help.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.signature.PlainTextMessageSigner;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.HttpEntity;
import org.apache.http.client.HttpClient;
import org.apache.http.HttpResponse;
import org.apache.http.impl.client.DefaultHttpClient;
import java.net.URLEncoder;
import java.util.UUID;
import java.security.GeneralSecurityException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.ClientProtocolException;
String uuid_string = UUID.randomUUID().toString();
uuid_string = uuid_string.replaceAll("-", "");
String keySecret = new String(Base64.encodeBase64("MyAccountOAuthSecret".getBytes()));
String oauth_nonce = uuid_string; // any relatively random alphanumeric string will work here. I used UUID minus "-" signs
String oauth_timestamp = "1318467427";
final String CONSUMER_KEY = "MyLogin";
final String CONSUMER_SECRET = "MyAccountOAuthSecret";
final String APPLICATION_NAME = "signpost-test";
final String freshbooks_REQUEST_TOKEN_URL = "
https://MyLogin.freshbooks.com/oauth/oauth_request.php";
final String freshbooks_ACCESS_TOKEN_URL = "
https://MyLogin.freshbooks.com/oauth/oauth_access.php";
final String freshbooks_AUTHORIZE_URL = "
https://MyLogin.freshbooks.com/oauth/oauth_authorize.php";
String callbackUrl = "na";
OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
consumer.setMessageSigner(new PlainTextMessageSigner());
OAuthProvider provider = new DefaultOAuthProvider(freshbooks_REQUEST_TOKEN_URL,
freshbooks_ACCESS_TOKEN_URL, freshbooks_AUTHORIZE_URL);
provider.setOAuth10a(true);
System.out.println("Fetching request token from freshbooks...");
// we do not support callbacks, thus pass OOB
String authUrl = provider.retrieveRequestToken(consumer, OAuth.OUT_OF_BAND
//callbackUrl
);
authUrl = OAuth.addQueryParameters(authUrl, OAuth.OAUTH_CONSUMER_KEY, CONSUMER_KEY,
"application_name", APPLICATION_NAME);
System.out.println("Request token: " + consumer.getToken());
System.out.println("Token secret: " + consumer.getTokenSecret());
//URL url = new URL("
https://MyLogin.freshbooks.com/api/2.1/xml-in -d '<request method=\"invoice.list\" />'");
URL url = new URL("
https://MyLogin.freshbooks.com/api/2.1/xml-in");
HttpURLConnection request = (HttpURLConnection) url.openConnection();
consumer.sign(request);
System.out.println("Sending request to freshbooks...");
String data = "OAuth realm=\"\", oauth_nonce=\"" + oauth_nonce
+ "\", oauth_signature_method=\"PLAINTEXT"
+ "\", oauth_timestamp=\"" + oauth_timestamp
+ "\", oauth_consumer_key=\"" + CONSUMER_KEY
+ "\", oauth_callback=\"none"
+ "\", oauth_token=\"" + consumer.getToken()
+ "\", oauth_signature=\"" + keySecret +"%2526"+ consumer.getTokenSecret()
+ "\", oauth_version=\"1.0\"";
System.out.println(data);
request.setRequestProperty("Authorization", data);
request.setRequestMethod("POST");
request.connect(); //i Get error here.
System.out.println("Response: " + request.getResponseCode() + " "
+ request.getResponseMessage());
Logs given below
-------------------------------------------
Fetching request token from freshbooks...
Request token: 4vk5BsJGchB9VHgxBMimJfZUEzVEMF9Eb
Token secret: pPWqaYRa7aLsxCBEZSbpnVc5xEziFk7nT
https://notapplicable-accounts.freshbooks.com/api/2.1/xml-inSending request to freshbooks...
OAuth realm="", oauth_nonce="162abd35739b48eaa342fea28e41b6c9", oauth_signature_method="PLAINTEXT", oauth_timestamp="1318467427", oauth_consumer_key="notapplicable-accounts", oauth_callback="none", oauth_token="4vk5BsJGchB9VHgxBMimJfZUEzVEMF9Eb",
oauth_signature="a3JhenNyVURnN1BDRjhNSnhFQnM1YmR3WjdxVXRZV2Jj%2526pPWqaYRa7aLsxCBEZSbpnVc5xEziFk7nT", oauth_version="1.0"
Response: 401 Unauthorized
[statistics] disconnected