how do you get the ticket_id on vimeo api with scribe - java

I am trying to upload video to vimeo and I understand that you need the ticket_id in order to be able to upload.
The think is I can not figure out how to get this ticket_id by using scribe.
Does anyone have any example how to do this?
Thanks in advance.
When I use:
OAuthRequest request = new OAuthRequest(Verb.GET, "http://vimeo.com/api/rest/v2");
request.addQuerystringParameter("method", "vimeo.videos.upload.getTicket");
this results in:
<err code="401" expl="The consumer key passed was not valid." msg="Invalid consumer key"/>
When I use method:
request.addQuerystringParameter("method", "vimeo.videos.upload.getQuota");
everything works fine. I tried putting some fake api key in the vimeo.videos.upload.getQuota method. That also resulted in invalid key. So it is not like method vimeo.videos.upload.getQuota does not need the api key. In fact it does and if you don't provide the valid key it wil not work. Somehow when calling method vimeo.videos.upload.getTicket, with the same api key that works for the mehod getQuota, I get response:
<err code="401" expl="The consumer key passed was not valid." msg="Invalid consumer key"/>
full code with fake api keys:
public class VimeoServiceConcept {
public static void main(String[] args) {
String apikey="api key";
String apisecret="secret";
String accessToken="access token";
String accessTokenSecret="access token secret";
OAuthService service = new ServiceBuilder()
.provider(VimeoApi.class)
.apiKey(apikey)
.apiSecret(apisecret)
.build();
Token token = new Token(accessToken, accessTokenSecret);
OAuthRequest request = new OAuthRequest(Verb.GET, "http://vimeo.com/api/rest/v2");
// request.addQuerystringParameter("method", "vimeo.videos.upload.getQuota");
request.addQuerystringParameter("format", "xml");
request.addQuerystringParameter("method", "vimeo.videos.upload.getTicket");
request.addQuerystringParameter("upload_method", "post");
service.signRequest(token, request);
System.out.println(request.getCompleteUrl());
Response response = request.send();
System.out.println("Got it! Lets see what we found...");
System.out.println(response.getHeader("code"));
System.out.println(response.getCode());
System.out.println(response.getBody());
}
}

Try getting the ticket after you get the quota. I have never tried getting the ticket without the quota first because their documentation explicitly states you need to check quota before you get the ticket. It looks like you just comment out what you're not testing. Try this instead:
public class VimeoServiceConcept {
public static void main(String[] args) {
String apikey="api key";
String apisecret="secret";
String accessToken="access token";
String accessTokenSecret="access token secret";
OAuthService service = new ServiceBuilder().provider(VimeoApi.class).apiKey(apiKey).apiSecret(apiSecret).build();
OAuthRequest request;
Response response;
accessToken = new Token("your_token", "your_tokens_secret");
accessToken = checkToken(vimeoAPIURL, accessToken, service);
if (accessToken == null) {
return;
}
// Get Quota
request = new OAuthRequest(Verb.GET, vimeoAPIURL);
request.addQuerystringParameter("method", "vimeo.videos.upload.getQuota");
signAndSendToVimeo(request, "getQuota", true);
// Get Ticket
request = new OAuthRequest(Verb.GET, vimeoAPIURL);
request.addQuerystringParameter("method", "vimeo.videos.upload.getTicket");
request.addQuerystringParameter("upload_method", "streaming");
response = signAndSendToVimeo(request, "getTicket", true);
//... the rest of your code...
}
}
Here's checkToken:
/**
* Checks the token to make sure it's still valid. If not, it pops up a dialog asking the user to
* authenticate.
*/
private static Token checkToken(String vimeoAPIURL, Token vimeoToken, OAuthService vimeoService) {
if (vimeoToken == null) {
vimeoToken = getNewToken(vimeoService);
} else {
OAuthRequest request = new OAuthRequest(Verb.GET, vimeoAPIURL);
request.addQuerystringParameter("method", "vimeo.oauth.checkAccessToken");
Response response = signAndSendToVimeo(request, "checkAccessToken", true);
if (response.isSuccessful()
&& (response.getCode() != 200 || response.getBody().contains("<err code=\"302\"")
|| response.getBody().contains("<err code=\"401\""))) {
vimeoToken = getNewToken(vimeoService);
}
}
return vimeoToken;
}
Here's getNewToken:
/**
* Gets authorization URL, pops up a dialog asking the user to authenticate with the url and the user
* returns the authorization code
*
* #param service
* #return
*/
private static Token getNewToken(OAuthService service) {
// Obtain the Authorization URL
Token requestToken = service.getRequestToken();
String authorizationUrl = service.getAuthorizationUrl(requestToken);
do {
String code = JOptionPane.showInputDialog("The token for the account (whatever)" + newline
+ "is either not set or is no longer valid." + newline
+ "Please go to the URL below and authorize this application." + newline
+ "Paste the code you're given on top of the URL here and click \'OK\'" + newline
+ "(click the 'x' or input the letter 'q' to cancel." + newline
+ "If you input an invalid code, I'll keep popping up).", authorizationUrl + "&permission=delete");
if (code == null) {
return null;
}
Verifier verifier = new Verifier(code);
// Trade the Request Token and Verfier for the Access Token
System.out.println("Trading the Request Token for an Access Token...");
try {
Token token = service.getAccessToken(requestToken, verifier);
System.out.println(token); //Use this output to copy the token into your code so you don't have to do this over and over.
return token;
} catch (OAuthException ex) {
int choice = JOptionPane.showConfirmDialog(null, "There was an OAuthException" + newline
+ ex + newline
+ "Would you like to try again?", "OAuthException", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.NO_OPTION) {
break;
}
}
} while (true);
return null;
}
Here's signAndSend:
/**
* Signs the request and sends it. Returns the response.
*
* #param request
* #return response
*/
public static Response signAndSendToVimeo(OAuthRequest request, String description, boolean printBody) throws org.scribe.exceptions.OAuthException {
System.out.println(newline + newline
+ "Signing " + description + " request:"
+ ((printBody && !request.getBodyContents().isEmpty()) ? newline + "\tBody Contents:" + request.getBodyContents() : "")
+ ((!request.getHeaders().isEmpty()) ? newline + "\tHeaders: " + request.getHeaders() : ""));
service.signRequest(accessToken, request);
printRequest(request, description);
Response response = request.send();
printResponse(response, description, printBody);
return response;
}

Related

How to get the access_token first in Apex Restful service to call GET/POST methods from outside?

I am doing Salesforce trailhead from the link : https://trailhead.salesforce.com/modules/apex_integration_services/units/apex_integration_webservices.
In this tutorial they've use access_token to call the GET request. But they have not guided us how to get the access_token, which is an important steps to call the APEX Rest from outside.
I tied to do something like below its saying me the error:
https://ap5.salesforce.com/services/oauth2/token?client_id="3MVG9d8..z.hDcPJZPIzGJ5UZDuKCOqbH8CCGCPnmwQuRbwLZ_2f.thbqWMX82H7JRGx4
6VYyEkuwzQ9._ww5"&client_secret="1180508865211885204"&username="pXXXXXXXXXXXXXXX.com"&password="AgXXXXXXXX"&grant_type=password
I understood the concept now and thanks for sharing other links.
client_id, client_secret, username, password and grant_type should be sent in a HTTP POST body not in header.
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type','application/x-www-form-urlencoded');
req.setEndpoint('https://ap5.salesforce.com/services/oauth2/token');
String CLIENT_ID = 'XXXXXXXXXXXXXXXXXXXXXXXXX';
String CLIENT_SECRET = 'XXXXXXXXXXXXXXXXXXXXXXX';
String USERNAME = 'XXXXXXXXXXXXXX';
String PASSWORD = 'XXXXXXXXXXXXXX';
req.setBody('grant_type=password' + '&client_id='+CLIENT_ID +
'&client_secret='+CLIENT_SECRET + '&username='+USERNAME + '&password='+PASSWORD);
Http http = new Http();
HTTPResponse response = http.send(req);
System.debug('Body ' + response.getBody());
System.debug('Status ' + response.getStatus());
System.debug('Status code ' + response.getStatusCode());
You might need to call api to get the access token.
Here's my code in C# to get the access token
async public static Task GetAccessTokenByUserNamePasswordAuthenticationFlowAsync(string username, string password, string token, string consumerKey, string consumerSecret)
{
HttpClient authClient = new HttpClient();
string sfdcConsumerKey = consumerKey;
string sfdcConsumerSecret = consumerSecret;
string sfdcUserName = username;
string sfdcPassword = password;
string sfdcToken = token;
string loginPassword = sfdcPassword + sfdcToken;
HttpContent content = new FormUrlEncodedContent(new Dictionary<string, string>
{
{"grant_type","password"},
{"client_id",sfdcConsumerKey},
{"client_secret",sfdcConsumerSecret},
{"username",sfdcUserName},
{"password",loginPassword}
}
);
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11; //tuanv2t: Salesforce has changed to use TLS 1.1 ->
//tuanv2t: Without, responseString will like this {"error":"unknown_error","error_description":"retry your request"}
HttpResponseMessage message = await authClient.PostAsync("https://login.salesforce.com/services/oauth2/token", content);
string responseString = await message.Content.ReadAsStringAsync();
//JObject obj = JObject.Parse(responseString);
//var oauthToken = (string)obj["access_token"];
//var serviceUrl = (string)obj["instance_url"];
var result = new GetAccessTokenResponse();
result.HttpResponseMessage = message;
//Convert json string into object
var accessTokenAPI = JsonConvert.DeserializeObject<AccessTokenAPI>(responseString);
if (accessTokenAPI != null)
{
result.AccessToken = new AccessTokenModel();
result.AccessToken.AccessToken = accessTokenAPI.access_token;
result.AccessToken.Id = accessTokenAPI.id;
result.AccessToken.InstanceUrl = accessTokenAPI.instance_url;
result.AccessToken.IssuedAt = accessTokenAPI.issued_at;
result.AccessToken.Signature = accessTokenAPI.signature;
result.AccessToken.TokenType = accessTokenAPI.token_type;
}
return result;
}
It's able to download all my source code example here ( include SOAP API as well )
https://bitbucket.org/tuanv2t/salesforceapidemo

Is restFB generated token is genuine?

I working with restFB for calling FaceBook Graph api to login, get posts and etc of users.
Created an app in developers account of Facebook.
Tried to call user details, with token generated here,
https://developers.facebook.com/tools/explorer?method=GET&path=8560751784547897&version=v2.8
For Example: accessToken from the above url provides = EAA....F
I am able to get all user details, when I am hitting url in browser as follows,
https://graph.facebook.com/v2.8/me?fields=id,name,email,birthday?access_token=EAA....f
I am getting following response in browser,
{
"id": "1127949",
"name": "youtr name",
"email": "youmail\u0040gmail.com"
"birthday": "10/27/1998"
}
I used following code to generate accesstoken dynamically,
StringBuffer callbackURLbuffer = request.getRequestURL();
int index = callbackURLbuffer.lastIndexOf("/");
callbackURLbuffer.replace(index, callbackURLbuffer.length(), "").append("/callback");
callbackURL = URLEncoder.encode(callbackURLbuffer.toString(), "UTF-8");
String authURL = "https://graph.facebook.com/oauth/authorize?client_id="
+ facebookAppId
+ "&redirect_uri="
+ callbackURL
+ "&scope=user_about_me,"
+ "user_actions.books,user_actions.fitness,user_actions.music,user_actions.news,user_actions.video,user_activities,user_birthday,user_education_history,"
+ "user_events,user_photos,user_friends,user_games_activity,user_groups,user_hometown,user_interests,user_likes,user_location,user_photos,user_relationship_details,"
+ "user_relationships,user_religion_politics,user_status,user_tagged_places,user_videos,user_website,user_work_history,ads_management,ads_read,email,"
+ "manage_notifications,manage_pages,publish_actions,read_friendlists,read_insights,read_mailbox,read_page_mailboxes,read_stream,rsvp_event";
In my callbackURL servlet, I am getting accessToken with code value, as follows,
StringBuffer redirectURLbuffer = request.getRequestURL();
int index = redirectURLbuffer.lastIndexOf("/");
redirectURLbuffer.replace(index, redirectURLbuffer.length(), "").append("/callback");
redirectURL = URLEncoder.encode(redirectURLbuffer.toString(), "UTF-8");
code = request.getParameter("code");
if(null!=code) {
accessURL = "https://graph.facebook.com/oauth/access_token?client_id=" + facebookAppId +
"&redirect_uri=" + redirectURL + "&client_secret=" + facebookAppSecret + "&code=" + code;
webContent = getWebContentFromURL(accessURL);
accessToken = getAccessTokenFromWebContent(webContent);
other functions used for this above code,
private static String getWebContentFromURL(String webnames) {
try {
URL url = new URL(webnames);
URLConnection urlc = url.openConnection();
//BufferedInputStream buffer = new BufferedInputStream(urlc.getInputStream());
BufferedReader buffer = new BufferedReader(new InputStreamReader(urlc.getInputStream(), "UTF8"));
StringBuffer builder = new StringBuffer();
int byteRead;
while ((byteRead = buffer.read()) != -1)
builder.append((char) byteRead);
buffer.close();
String text=builder.toString();
return text;
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
And,
private static String getAccessTokenFromWebContent (String webContent) {
String accessToken = null;
int s = webContent.indexOf("access_token=") + ("access_token=".length());
int e = webContent.indexOf("&");
accessToken = webContent.substring(s, e);
return accessToken;
}
Lets say this accessToken = EAA....2
If I call the below url with this Token,
https://graph.facebook.com/v2.8/me?fields=id,name,email,birthday&access_token=EAA....2
I am getting following response,
{
"id": "1127949",
"name": "youtr name",
"email": "youmail\u0040gmail.com"
}
If I hit following url, with bio feild, I got error msg in json as follows,
{
"error": {
"message": "(#12) bio field is deprecated for versions v2.8 and higher",
"type": "OAuthException",
"code": 12,
"fbtrace_id": "ARkoFJVP/Jk"
}
}
can some one say why this is happening for me.
Take a look here, to get about the user permission.
https://developers.facebook.com/docs/facebook-login/permissions/#reference-user_about_me.
As per your comment, regarding debuggin your token, sounds like you didn't get the permission enabled for user_about_me by facebook.
You have to send Facebook for a review about your app and need to approve by Facebook, then only you can access the user details and other things.
Follow these steps,
Go to - https://developers.facebook.com/apps/your_app_id/review-status/
Click on Start a submission button.
Select the check boxes,which you needs for permission. Then Click add Items button.
Then click on Edit Notes for each permission.
Here, breif about your app with this permission and upload a video of demo of your app.
Facebook will review it and accept/reject as per the norms.
This is what you are missing and it causes for your problem..

Jax-RS and Xmlhttp Communication

I have a REST Server in Java JAX-RS and an HTML page.
I want to send a JSON array, a username, and an accountID from the HTML page through an xmlhttp POST request by making all of them a single big String so I can use the xmthttp.send() method.
The HTML sending code is:
function sendData() {
var req = createRequest();
var postUrl = "rest/hello/treeData";
var dsdata = $("#treeview").data("kendoTreeView").dataSource.data();
var accID = "onthespot";
var username = "alex";
req.open("post", postUrl, true);
req.setRequestHeader("Content-type","text/plain");
req.send("data=" + JSON.stringify(dsdata) + "&username=" + username + "&accID=" + accID);
req.onreadystatechange = function() {
if (req.readyState != 4) {
return;
}
if (req.status != 200) {
alert("Error: " + req.status);
return;
}
alert("Sent Data Status: " + req.responseText);
}
}
And the Server JAX-RS code is:
#Path("/treeData")
#POST
#Consumes(MediaType.TEXT_PLAIN)
#Produces(MediaType.TEXT_PLAIN)
public String storeTreeData(
#QueryParam("data") String data,
#QueryParam("username") String username,
#QueryParam("accID") String accID) {
System.out.println("Data= " + data + "\nAccID= " + accID + "\nUsername= " + username);
return "Done";
}
The problem is that all the variables are printed as null..
the storeTreeData function should find the data , username , accID variables through #QueryParam and store them isn't that right?
Anyone know what's the problem here?
PS:The xmlhttp request is initiallized correctly and the connection is made but the parameters are not passed on the server.
What you try to do:
#QueryParam is used to get parameters from the query of the request:
http://example.com/some/path?id=foo&name=bar
In this example id and name can be accessed as #QueryParam.
But you are sending the parameters in the body of your request.
What you should do:
To get the parameters from the body, you should use #FormParam together with application/x-www-form-urlencoded:
#Path("/treeData")
#POST
#Consumes(MediaType.APPLICATION_FORM_URLENCODED)
#Produces(MediaType.TEXT_PLAIN)
public Response storeTreeData(
#FormParam("data") String data,
#FormParam("username") String username,
#FormParam("accID") String accID) {
// Build a text/plain response from the #FormParams.
StringBuilder sb = new StringBuilder();
sb.append("data=").append(data)
.append("; username=").append(username)
.append("; accId=").append(accID);
// Return 200 OK with text/plain response body.
return Response.ok(sb.toString()).build();
}
Edit:
You should also use
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
in your JavaScript code.

fetch profile info from google plus in java error

i am trying to authenticate and and fetch user's profile data but i a getting below error,Please suggest.
// Generate the URL to which we will direct users
String authorizeUrl = new GoogleAuthorizationRequestUrl(CLIENT_ID,
CALLBACK_URL, SCOPE).build();
System.out.println("Paste this url in your browser: " + authorizeUrl);
// Wait for the authorization code
System.out.println("Type the code you received here: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String authorizationCode = in.readLine();
// Exchange for an access and refresh token
GoogleAuthorizationCodeGrant authRequest = new GoogleAuthorizationCodeGrant(
TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET,
authorizationCode, CALLBACK_URL);
authRequest.useBasicAuthorization = false;
AccessTokenResponse authResponse = authRequest.execute();
String accessToken = authResponse.accessToken;
GoogleAccessProtectedResource requestInitializer = new GoogleAccessProtectedResource(
accessToken, TRANSPORT, JSON_FACTORY, CLIENT_ID, CLIENT_SECRET,
authResponse.refreshToken);
// Set up the main Google+ class
Plus plus = new Plus(TRANSPORT, requestInitializer,JSON_FACTORY);
// Make a request to access your profile and display it to console
Person profile = plus.people.get("me").execute();
System.out.println("ID: " + profile.getId());
System.out.println("Name: " + profile.getDisplayName());
System.out.println("Image URL: " + profile.getImage().getUrl());
System.out.println("Profile URL: " + profile.getUrl());
error is:-
Exception in thread "main" java.lang.IllegalAccessError: tried to access field com.google.api.client.http.HttpRequest.method from class com.google.api.client.googleapis.MethodOverride
at com.google.api.client.googleapis.MethodOverride.overrideThisMethod(MethodOverride.java:99)
at com.google.api.client.googleapis.MethodOverride.intercept(MethodOverride.java:87)
at com.google.api.services.plus.ApiClient.buildHttpRequest(ApiClient.java:251)
at com.google.api.services.plus.Plus$RemoteRequest.execute(Plus.java:1055)
at com.google.api.services.plus.Plus$People$Get.executeUnparsed(Plus.java:991)
at com.google.api.services.plus.Plus$People$Get.execute(Plus.java:976)
at com.googleplus.test.TestAuth2.main(TestAuth2.java:64)
Have you tried to use the code from this starter project?

Digest authentication with Jersey Client

I have written a REST web service with Jersey Server (that totally rocks !).
I am now developing the client part of it, with Jersey Client as well.
On the server side, I have chosen a DIGEST authentication, because I personally think that BASIC authentication is an heresy that should be marked as "DEPRECATED" in our heads.
Unfortunately, I do not see any support of the Digest authentication on the client side.
For BASIC authentication, one does something like :
client.addFilter(
new HTTPBasicAuthFilter(
user,
password));
But I see no "HTTPDigestAuthFilter" counterpart.
Am I missing something ?
Thanks for your help,
Raphael
I have just implemented it.
I have created a feature request in the Jersey issue tracker, and posted my implementation there, as attachment :
https://jersey.dev.java.net/issues/show_bug.cgi?id=542
It works fine for communicating with a DIGEST authentication of a Tomcat server.
I have not tested for other web servers yet.
Here I wrote some random uri. Please fill your desired URI
For sample testing you can take help of google services which are available in the internet for open.
import javax.ws.rs.core.*;
import org.apache.commons.codec.digest.*;
import org.codehaus.jettison.json.*;
import com.sun.jersey.api.*;
public class DigestClient {
//Dividing into two parts because we need to send the last part of uri in our second request to service.
static String baseUri = "https://www.something.com";
static String subUri = "/later-part";
public static void main(String[] args) throws JSONException{
ClientConfig cc = new DefaultClientConfig();
Client client = Client.create(cc);
WebResource webResource = client.resource(baseUri+subUri);
ClientResponse response = webResource.get(ClientResponse.class);
// Basically in Digest-Authentication mechanism, we hit the rest service two times.
// First time with No Authentication, which returns some values (qop, nonce, realm) which are used as inputs in second call to rest service.
/*--------------- First call-----------------*/
// We get 401, Unauthorized
System.out.println(response.getStatus()+" "+response.getStatusInfo());
// Here is the complete header information
System.out.println(response.getHeaders());
// We need "WWW-Authenticate" part information for our second call to rest
System.out.println("WWW-Authenticate: \t" + response.getHeaders().get("www-Authenticate"));
String noAuthResp = response.getHeaders().get("www-Authenticate").toString();
noAuthResp = noAuthResp.replace("Digest ", "");
noAuthResp = noAuthResp.replace('[', '{');
noAuthResp = noAuthResp.replace(']', '}');
// Creating a JSONObject for easy information retrieval
JSONObject resp = new JSONObject(noAuthResp);
/*--------------- Second call-----------------*/
// Here client has to set the fields which was returned from the first call
String user = "postman"; // username
String password = "password"; // password
String realm = resp.getString("realm"); // realm value from the first rest-call response
String qop = resp.getString("qop"); //qop value from the first rest-call response
String nonce = resp.getString("nonce"); // nonce value from the first rest-call response
String opaque = resp.getString("opaque"); // Some times if we don't get this value, set it with ""
String algorithm = "MD5"; // The algorithm set by the client
int nonceCount = 678; // Some numerical input from the client
String clientNonce = "afdjas0"; // Some random text from the client for encryption
String method = "GET"; // HTTP method
String ha1 = new DigestClient().formHA1(user, realm, password);
String ha2 = new DigestClient().formHA2(method, subUri);
String responseCode = new DigestClient().generateResponse(ha1, nonce, nonceCount, clientNonce, qop, ha2);
// Header to be sent to the service
String value = "Digest username=\""+user+"\", realm=\""+realm+"\", nonce=\""+nonce+"\", uri=\""+subUri+"\", qop="+qop+", nc="+nonceCount+", cnonce=\""+clientNonce+"\", response=\""+responseCode+"\", opaque=\""+opaque+"\"";
// Hitting the service
response = webResource.header("authorization", value).type(MediaType.TEXT_PLAIN).accept("*").get(ClientResponse.class);
System.out.println("\nComplete Response:\n"+response+"\n");
String output = response.getEntity(String.class);
System.out.println("Response Text: "+output);
}
// For generating HA1 value
public String formHA1(String userName,String realm,String password){
String ha1 = DigestUtils.md5Hex(userName + ":" + realm + ":" + password);
return ha1;
}
// For generating HA2 value
public String formHA2(String method,String uri){
String ha2=DigestUtils.md5Hex(method + ":" + uri);
return ha2;
}
// For generating response at client side
public String generateResponse(String ha1,String nonce,int nonceCount,String clientNonce,String qop,String ha2){
String response=DigestUtils.md5Hex(ha1 + ":" + nonce + ":" + nonceCount + ":" +clientNonce +":" + qop + ":" +ha2);
return response;
}
}

Categories