Connect MS Dynamics CRM 2011 from java - java

I am trying to connect to MS Dynamics CRM 2011 from Java. After lots of searching I came across one link in MS forums which gives a code snippet to invoke MS Dynamics CRM 2011 WS from Java.
Consuming CRM REST Service from Java
However, it does not provide much details other than an account creation step. Using this code snippet I am able to create accounts. However, I want to also use the same REST web service to retrieve accounts, create new case request, add case request to account, etc. Based on .NET examples available online I am trying to use the service.createAccountQuery() method to retrieve accounts. However, while all .NET examples uses LINQ to setup search criteria, I am clueless how to specify the subpath string in java to retrieve existing Accounts by their name/city/country etc.
Appreciate any help.

Instead of the "Account act..." line and below, use something like:
// Retrieve all accounts that the user has read access to.
string fetch1 = #"<fetch mapping=""logical"">
<entity name=""account"">
<all-attributes/>
</entity>
</fetch>";
// Fetch the results.
ExecuteFetchRequest req = new ExecuteFetchRequest();
req.FetchXml = fetch1;
ExecuteFetchResponse result1 = (ExecuteFetchResponse)service.Execute(req);
I'm unfamiliar with this proxy, but you want to call service.X where X is the Execute or RetrieveMultiple (based on what the proxy provides).

not sure if you are still looking but for anyone else looking. I had the same issue.
It is using the odata query url so the subpath is the entitySet (e.g. for account it would be AccountSet)
you can then set the filter using the .filter method and the select via the .select method.
I did notice that you can't seem to do these on seperate lines though
e.g. you can't do
Query<microsoft.crm.sdk.data.services.Account> q = service.createAccountQuery("AccountSet");
q.filter("substringof('Test',Name)");
q.select("AccountId,Name");
q.execute();
you must do
Query<microsoft.crm.sdk.data.services.Account> q = service.createAccountQuery("AccountSet").filter("substringof('Test',Name)").select("AccountId,Name");
q.execute();
Chris

Related

Cloudant Unathorized request

I've been trying to work this problem out about accessing my cloudant DB from IBM-bluemix services.
I've set my URL in Android studio like this:
URL url = new URL("https://XXXX-XXXX-XXXX-XXXX-XXXX-bluemix.cloudant.com");
And then trying to access it by:
CloudantClient client = ClientBuilder.url(url)
.username("myusername")
.password("mypassword")
.build();
However, When i try to access/modify anything within my database,
ex:
client.createDB("test_DB");
I get the error:
Error: unauthorized. Reason: one of _admin, server_admin is required for this request.
What am i missing?
I've seen many different ways to approach querying from cloudant, but this is the closest i've gotten.
Have i just completely misunderstood how querying from an existing database works?
If you are using a Cloudant legacy API key you won't be able to perform account administrator level actions, such as listing or creating databases because the legacy API keys are associated with specific databases not the account. If that is the type of credentials you are using you can initialize the client in the way you have done and get the existing database via:
Database db = client.database("test_DB", false);
Try your credentials using curl on the command line (see https://cloud.ibm.com/docs/services/Cloudant/tutorials?topic=cloudant-databases#databases)
Verify if your account accepts legacy credentials ("username and password") or IAM tokens.
See https://cloud.ibm.com/docs/services/Cloudant/guides?topic=cloudant-ibm-cloud-identity-and-access-management-iam-#ibm-cloud-identity-and-access-management-iam-

How to write a Java code to read fields from a website that requires login and uses POST request?

Need some help with fetching some data from a website.
Previously , we had following code in our application and it used to fetch the required data. We just used to read the required fields by forming a URL by passing username , password and search parameter (DEA number). The same URL (with parameters ) could also be hit from browser directly to see the results. It was a simple GET request:
{URL url = new URL(
"http://www.deanumber.com/Websvc/deaWebsvc.asmx/GetQuery?UserName="+getUsername()+"&Password="+getPassword()+"&DEA="
+ deaNumber
+ "&BAC=&BASC=&ExpirationDate=&Company=&Zip=&State=&PI=&MaxRows=");
Document document = parser.parse(url.toExternalForm());
// Ask the document for a list of all <sect1> tags it contains
NodeList sections = document.getElementsByTagName("DEA");
//Followed by a loop code to get each element by using sections.item(index).getFirstChild() etc.
}
Now, the website URL has got changed to following:
https://www.deanumber.com/RelId/33637/ISvars/default/Home.htm
I am able to login to the URL with credentials , go to the search page , enter the DEA number and search. The login page comes as a pop-up once I click 'Login' link on home page. Also, the final result comes as a pop-up. This is a POST request so I am unable to form the complete URL which I could use in my code.
I am not an expert in Web Services , but I think I need a web service URL like the one mentioned in the code above. Not sure how to get that !! Even if I get the URL , I am not sure how to perform the login through Java code and search the DEA number.
Also, it would be great if I could validate the URL manually before using in Java. Let me know if there is any way.
Or, in case there is any alternate approach in Java; kindly suggest.
Thanks in advance.
First of all, the previous approach provided by the website was completely wrong and insecure, because it passes the username and password as querystring parameters in plain text. I think, they would have realized this thing and changed their way of authentication.
Also, it looks like that they have restricted the direct URL based requests from the client applications like yours. For such requests from clients, they have published the web services. Check this link. They also have mentioned the rates for web service request counts.
So, you may need to open a formal communication channel to get authentication and other details to access their web services for this purpose. Depends on what they use for web service client authentication, you may code your client to access the web services.
I hope this helps.

How can i get GA client id within jsp or java code

I need to send google analytics client id to server side where we will be running google measurement protocol API.
We have our GA implemented via GTM.
As per google recommendation, i am not supposed to parse _ga cookie hence i tried going through the recommended way. if i paste the code below on the console, it works fine but give me error ga reference is not defined as soon as i include this code within my jsp.
trackers = ga.getAll();
var i, len;
for (i = 0, len = trackers.length; i < len; i += 1) {
if (trackers[i].get('trackingId') === "UA-62222232-2") {
console.log(trackers[i].get('clientId'));
}
}
Please let me know what is the correct way of getting ga client id? i am unable to do so via jsp and thinking if it would be better to parse cookie within java code.
TIA
If you put the above code in a jsp file it becomes simply Javascript in the browser, so that's what you need to debug.
But I actually recommend that you ignore Googles' warning and simply parse the cookie. Even if you retrieve the clientId via Javascript you'd need to pass it on to the server, having the cookie parsed by server-side code solves that problem.
If you really want a fool-proof solution you can create and maintain a clientId in your Java code and pass it into the GA tag (e.g. via a datalayer variable since you use GTM). Since then you control the format for the client id yourself you can be sure that there are no surprises.

Posting tweets from a java application

I want to auto tweet from a java application. What is the simplest way to do it? Can i avoid using libraries like Twitter4j etc.,
I need an implementation for a simple api like
Tweet(username, password, message)..
Thank you.
I recommend you to use twitter4j and using this you can create oAuth requests easily.
Twitter rate limits apply to desktop application and it is 150/hour.
Twitter does not support basic authentication with username and password anymore.
You are required to create an application in twitter and using the consumer key and secret only you can access your twitter account.
If you are going to access the twitter by a desktop application then you have to select
Application Type: as "Client" while creating the application.
Then you can use the syntax below to update your status in twitter
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey(consumerKey)
.setOAuthConsumerSecret(consumerSecret)
.setOAuthAccessToken(oAuthAccessToken)
.setOAuthAccessTokenSecret(oAuthAccessTokenSecret);
TwitterFactory tf = new TwitterFactory(cb.build());
Twitter twitter = tf.getInstance();
twitter.updateStatus("This is a test message"); //ThrowsTwitterException
I hope this helps you... Please let me know if this is not the answer you were looking for.
Implementing your own oAuth request involve creating signature that for me was complicated and it is sensitive to time and time format that we send.
Twitter has a REST web API, and a lot of documentation. For reference:
http://dev.twitter.com/doc
While you don't necessarily need Twitter4J, it does make it easier. Otherwise you would need to assemble your own URL requests and take care of authentication. They offer more than one style:
http://dev.twitter.com/pages/auth_overview
Traditionally, OAuth is the preferred style for desktop application to web server integration--but that protocol is a bit complicated.
There's nothing that says you can't create your Tweet() method to hide away the details of using Tweet4J or hand-rolling the request yourself.
I want to auto tweet from a java
application.
I hope you are not spamming.. :D
Try this one: http://code.google.com/p/java-twitter/
You can wrap the example code into:
public void tweet(String username, String password, String message){
Api api = Api.builder().username(username).password(password).build();
api.updateStatus(message).build().post();
}
And then call it as tweet.(username,pass,message)
Looks simple to me.
Spring Social? I saw a demo of it at SpringOne - looked pretty cool, although I personally do not have a use for it, and therefore haven't done much besides read about it. You get some OAuth capability and templates for interacting with the major social networking sites out of the box.

Get a google's email/username in java?

is it possible to retrieve the username of a google account that i have succesfully authenticated using OAuth?
i have retrieved the users Access tokens but i am wondering if their is a API call i can make such has https://google.api/getUserName and pass the access tokens to that call and succesfully retrieve the users email/username?
In a normal OAuth web service, all you need is the secret and id access tokens to make calls to the web service but in google you also need the username too.
Any ideas?
Take a look at http://sites.google.com/site/oauthgoog/Home/emaildisplayscope . That should work for you.
The only way I figured so far is using the Spreadsheet API.
If you request the feed, that lists all documents
https://spreadsheets.google.com/feeds/spreadsheets/private/full?alt=json
There is a field with the username as well:
response.data.feed.title.$t
Unfortunately, this means prompting the user to grant access to his GDocs account, which may be confusing..
But I don't know of any API by Google to directly get the username.
Best way, the following feed was retrieved from the Contacts Data API:
https://www.google.com/m8/feeds/contacts/default/full?alt=json&max-results=0
and get next fields from the feed:
response.data.feed.id
or
response.data.feed.author.name.$t
response.data.feed.author.email.$t
http://code.google.com/intl/ja/apis/accounts/docs/AuthForInstalledApps.html#Errors
This one is PHP, i think a slight modification in JAVA could make this workout
http://www.electrictoolbox.com/google-analytics-login-php-curl-username-password/

Categories

Resources