function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
tartanarmytartanarmy 

OAuth userId

How do I get the username/userId after logging in using OAuth to use in subsequent queries?

 

 

Best Answer chosen by Admin (Salesforce Developers) 
SuperfellSuperfell

You were using the ios toolkit, which still exists, and is a different framework to zksforce. (the ios toolkit forked zksforce, hence the similar names of classes). I don't know if the ios toolkit supports getUserInfo, you'd have to check the source.

 

One of the benefits of oauth is that you don't need to give your credentials to the 3rd party app, that benefit is reduced if the app uses a webview for the oauth flow, as then you're still entering your credentials into the the process of the 3rd party app, and in theory, it could access and store them. By using a browser (as originally intended by oauth), the user never enters their credentials into the process of the 3rd party app.

All Answers

SuperfellSuperfell

You can use the Id service to get it (do a GET on the url returned in the 'id' property in the oauth response)

cloudcodercloudcoder

Once you are authenticated, you will be provided an oauth token which you can use for subsequent queries. For example if you are using the REST API, you would add the oauth token in the "Authorization" header.  Here is an example from Ruby:

 

headers 'Authorization' => "OAuth my-oauth-token"

 

 

tartanarmytartanarmy

Thanks for the replies!  I am developing using iOS for the iPad using the zkSForce toolkit.    There is a UserInfo class but not sure if that holds the information I need or whether the GET method above is what I should go for?

SuperfellSuperfell

If you're using the soap API, then there's a getUserInfo call, the latest version of zkSforce will automatically call it for you if you call currentUserInfo (normally this is populated from the login call, but with oauth, you don't call login), so you can then get the username from that, something like [[client currentUserInfo] userName]

tartanarmytartanarmy

I was previously using the ZKServerSwitchboard switchboard in earlier versions of the SOAP API.   With the latest version of the code this appears to have changed so  should I now should be using the ZKSforceClient or can I still get the userId using the switchboard code?

 

Also - in your latest examples you launch a browser instead of authenticating via a UIWebView - is there a reason for this?

 

Cheers

SuperfellSuperfell

You were using the ios toolkit, which still exists, and is a different framework to zksforce. (the ios toolkit forked zksforce, hence the similar names of classes). I don't know if the ios toolkit supports getUserInfo, you'd have to check the source.

 

One of the benefits of oauth is that you don't need to give your credentials to the 3rd party app, that benefit is reduced if the app uses a webview for the oauth flow, as then you're still entering your credentials into the the process of the 3rd party app, and in theory, it could access and store them. By using a browser (as originally intended by oauth), the user never enters their credentials into the process of the 3rd party app.

This was selected as the best answer
tartanarmytartanarmy

Thanks Simon!  I am now using the ZKSforce framework and getting the userId as you described.

 

Wonder if you could help with one other task:

 

If I want to update a record that I have retrieved via a ZKQueryResult - what is the best way to do this with ZKSforce? e.g update a date on an Opportunity?

 

SuperfellSuperfell

Create a new ZKSObject, set the fieldValues you want to change, and call update on the ZKSforceClient class, something like

 

 

 ZKSObject *contact = [ZKSObject withTypeAndId:@"Contact" sfId:@theRecordId];
 [contact setFieldValue:@"Fell" field:@"LastName"];
 [contact setFieldValue:@"Simon" field:@"FirstName"];
 NSArray *results = [sforce update:[NSArray arrayWithObject:contact]];
 ZKSaveResult *sr = [results objectAtIndex:0];
 if ([sr success])
    NSLog(@"updated contact id %@", [sr id]);
 else
    NSLog(@"error updating contact %@ %@", [sr statusCode], [sr message]);

 

 

tartanarmytartanarmy

Many thanks - will let you know how I get on.

tartanarmytartanarmy

Hi Simon - I have a new issue that I was wondering if you could assist with.

 

When carrying out the query Select Id, (Select ActivityDate FROM OpenActivities) FROM Opportunity WHERE Id = 'XXXXX'

 

I am unsure if the code provided can handle this to loop through the OpenActivities on an account?


Do you have an example or know if this can be processed?

 


Thanks in advance.