I am not able to get access token from salesforce instance, Can anyone help me out from this.
I have tried many different ways, but not able to actually get it done :(.
HttpClient httpclient2 = new DefaultHttpClient();
HttpPost post = new HttpPost("https://instance.salesforce.com/services/oauth2/token");
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("client_id", CONSUMER_KEY));
params.add(new BasicNameValuePair("client_secret", SECRET_KEY));
params.add(new BasicNameValuePair("grant_type", "password"));
params.add(new BasicNameValuePair("username", "emailaddress"));
params.add(new BasicNameValuePair("password", password+securityToken));
post.setEntity(new UrlEncodedFormEntity(params));
HttpResponse response = httpclient2.execute(post);
String body = EntityUtils.toString(response.getEntity());
System.out.println(body);
It is giving below error :
{"error":"invalid_grant","error_description":"authentication failure"}
Thanks in advance.
I believe that the problem is you need to use login.salesforce.com as the login host, not <instance>.salesforce.com but if that doesn't work, here is a complete example that uses httpclient to login and then make a query: https://github.com/jamesward/salesforce-rest-starter
It has over 2 days but Finally, I have done it :).
Program was correct but there was missing something with Configuration from Salesforce side. Below is the missing configuration which fixed my issue.
Thanks.
Related
I need to update the permissions of a Drive document from Salesforce.
I wanted to use Named Credentials, but I didn't find any way of building a call like this one:
https://www.googleapis.com/drive/v3/files/{documentId}/permissions
where {documentId} is a dynamic value.
I've seen that it is possible to add a prefix, but actually even if I create a Named Credential with only https://www.googleapis.com/drive/v3/files, when I call it from my Apex class I get a permission error.
Is there a way to achieve what I would like or I need to change approach?
Thank you
What exactly error are you getting? Salesforce security about not having access to class X? Something about callouts not allowed from triggers? You're sure it works with hardcoded document id?
Should be possible to make the named credential point to https://www.googleapis.com or https://www.googleapis.com/drive/v3/files/ and then add the rest of the endpoint in Apex. If it throws errors - maybe Drive's API is special, you'd need to read up.
String endpoint = 'callout:MyNamedCredential' + '/abc123/permissions';
HttpRequest req = new HttpRequest();
req.setEndpoint(endpoint);
req.setMethod('GET');
I have something like that:
Named credential pointing to https://maps.googleapis.com/maps/api/geocode/json
and then
static final String ENDPOINT = 'callout:GoogleMaps?key={0}&latlng={1}&result_type=premise%7Cstreet_address';
String apiKey = SomeCustomSetting__c.getInstance().GoogleApiKey__c;
String latLng = '60.23,11.17';
req.setEndpoint(String.format(ENDPOINT, new List<String>{apiKey, latLng}));
HttpResponse res = h.send(req);
You could also look into "Files Connect" API I guess.
I am using the python3.7.2 module simple-salesforce==0.74.2 and I am having trouble trying to establish a connection to my salesforce sandbox. I can login to the salesforce production with the same credentials just fine like so:
from simple_salesforce import Salesforce
sf = Salesforce(username='user#domain.com', password='pswd', security_token='mytoken')
Okay cool. Now I attempt to login to my sandbox with the following:
sf = Salesforce(username='user#domain.com.sandbox_name', password='pswd', security_token='mytoken', sandbox=True)
And I get the error:
simple_salesforce.exceptions.SalesforceAuthenticationFailed:
INVALID_LOGIN: Invalid username, password, security token; or user
locked out.
So I tried logging in with a different method:
sf = Salesforce(username='user#domain.com.sandbox_name', password='pswd', security_token='mytoken', domain='sandbox_name')
And this gave a different error:
requests.exceptions.ConnectionError:
HTTPSConnectionPool(host='sandbox_name.salesforce.com', port=443): Max
retries exceeded with url: /services/Soap/u/38.0 (Caused by
NewConnectionError(': Failed to establish a new connection: [Errno 8]
nodename nor servname provided, or not known'))
I am using a Developer sandbox, named sandbox_name, following salesforce's instructions. Can someone give some advice on what I am doing incorrectly?
Solved. Set domain='test' and generate a new token under your sandbox account
this didn't work for me, but what did was:
`sf = Salesforce(
username='my.email#test.com',
password='sadfd8d8d8',
security_token='d8d8asd8f8d8',
instance_url='https://my-dev-org-instance-dev-ed.my.salesforce.com')`
The advice here may be a bit deprecated. After a bit of tinkering, I was able to get the simple_salesforce library working with the Salesforce sandbox on a custom domain with the following code. Note the domain that I am passing to the api as well the sand_box name that needs to be appended to the username.
from simple_salesforce import Salesforce
USER = "user#domain.com.sandbox_name"
PASS = "pass"
SEC_TOKEN = "token"
DOMAIN = "<domain>--<sandbox_name>.<instance>.my"
sf = Salesforce(username=USER, password=PASS, security_token=SEC_TOKEN, domain=DOMAIN)
The call to the newer version of com.google.cloud.pubsub.spi.v1.Publisher.publish(pubsubMessage).get() is hanging forever. I'm not sure what the problem is.
Code snippet:
com.google.cloud.pubsub.spi.v1.Publisher publisher = Publisher.defaultBuilder(TopicName.parse("projects/" + projectId + "/topics/" + topicName))
.setChannelProvider(TopicAdminSettings
.defaultChannelProviderBuilder()
.setCredentialsProvider(FixedCredentialsProvider.create(ServiceAccountCredentials.fromStream(new FileInputStream(keyFile))))
.build())
.build();
ApiFuture<String> messageIdFuture = publisher.publish(pubsubMessage);
messageIdFuture.get() // HANGS FOREVER!!
The older API works fine where we do:
GoogleCredential credential = new GoogleCredential.Builder()
.setTransport(new NetHttpTransport())
.setJsonFactory(JSON_FACTORY)
.setServiceAccountId(serviceAccount)
.setServiceAccountScopes(Arrays.asList(PubsubScopes.PUBSUB))
.setServiceAccountPrivateKeyFromP12File(new File(keyFile))
.build();
Pubsub pusub = new Pubsub.Builder(transport, JSON_FACTORY, credential).setApplicationName("bigquery").build();
PubsubMessage pubsubMessage = new PubsubMessage();
pubsubMessage.encodeData(message.getBytes());
PublishRequest publishRequest = new PublishRequest();
publishRequest.setMessages(Arrays.asList(pubsubMessage));
pubsub.projects().topics().publish(outputTopic, publishRequest).execute();
Can somebody point out what am I missing?
This may be because you have not configured subscription for the topic or given proper permissions in the GCP console.
It is required to have a subscription attached to the topic. Also make sure you give the correct permissions in the console. Note that you give this
"client_email" : (an auto-generated email id)
auto-generated email id with admin subscriber permissions in the console.
You will get this field in your projectname.json credentials file while configuring.
Hope it helps.
I have no idea why but once i've add a compile dependency for guava no more hangs on a get call.
compile group: 'com.google.guava', name: 'guava', version: '23.0'
For us, when we removed guava dependency, it worked. We are using following version for publishing:
com.google.cloud:google-cloud-pubsub:1.33.0
I am new to Resteasy, I am calling a service and getting the response successfully. I am able to print the response as well (which is the expected response).
ClientResponse<String> response= clientRequest.post(String.class);
System.out.println("response"+response.getEntity());
Console output is: response{"id":8,"displayName":"xyz_abc","roles":null, .....
But now I want to parse/map the response that I get from the service to a business object (like a User.java pojo) in client side application. I tried going through the docs but couldn't comprehend much. I tried googling, again not much help there. Please help me achieve this.
Thanks
Finally found the answer,
ClientResponse<String> response= clientRequest.post(String.class);
Gson gson = new Gson();
User user = gson.fromJson(response.getEntity(), User.class);
This solved my problem.
I am using twitter4j-core-2.1.2.jar.
Following is my code which executed after callback url hit.
Code
String token = (String) session.getAttribute("token");
String tokenSecret = (String)session.getAttribute("tokenSecret");
AccessToken accessToken = new AccessToken(token, tokenSecret);
Twitter twitter = new
TwitterFactory().getOAuthAuthorizedInstance(CONSUMER_KEY,CONSUMER_SECRET,accessToken);
// getting exception on this line.
User user = twitter.verifyCredentials();
Follwowing is the exception
java.lang.IncompatibleClassChangeError: Found interface twitter4j.User, but class was expected
at com.thefollowfriday.servlet.HomeServlet.doGet(HomeServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:693)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:806)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
Reason: TwitterException{statusCode=401, retryAfter=0,
rateLimitStatus=null} at
twitter4j.internal.http.HttpClientImpl.request(HttpClientImpl.java:
301) at
twitter4j.internal.http.HttpClientWrapper.request(HttpClientWrapper.java:
68) at
twitter4j.internal.http.HttpClientWrapper.get(HttpClientWrapper.java:
90) at twitter4j.Twitter.verifyCredentials(Twitter.java:1134) at
com.thefollowfriday.servlet.HomeServlet.doGet(HomeServlet.java:68) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:693) at
javax.servlet.http.HttpServlet.service(HttpServlet.java:806) at
Please help. I am stuck on it.
Status code 401 typically indicates a problem with credentials on the Twitter server, in this case while the server is trying to verify the user using the supplied credentials. Are you sure all the values you're passing are valid values (token or tokenSecret aren't null perhaps)?
I just ran into this problem. The issue was that I had two different Twitter4J library jars in my WEB-INF/lib folder. I removed one, restarted, and everything worked.