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
venchinn2venchinn2 

no viable alternative at character error in apex class

when i tried this code i got the above error

 

 

public class GoogleDrive
{
private static Http http;
private static HttpRequest httpReq;
private static HttpResponse httpRes;
private static void Initialize()
{
http = new Http ();
httpReq = new HttpRequest();
httpRes = new HttpResponse();
}
public static boolean getTokens(String tokenType)
{
String messageBody='';
Initialize();
httpReq.setMethod('POST');
httpReq.setEndpoint('https://accounts.google.com/o/oauth2/token');
httpReq.setHeader('content-type',
'application/x-www-form-urlencoded');

messageBody =
'client_id='+107266498774.apps.googleusercontent.com+
'&client_secret='+Heda08bh6mJLj6d3k5v3HFc2;

if(tokenType=='ALL')
messageBody = messageBody +
'&code='+4/GT1nC7wEx5nIxR5NLTG84oNOQFvc.ciFUSB7eh3AcXE-sT2ZLcbQhBE8nhQI+
'&redirect_uri='+https://login.salesforce.com/apex/GoogleDrive+
'&grant_type='+'authorization_code';

else if(tokenType=='ACCESS_TOKEN')
messageBody = messageBody +
'&refresh_token='+1/2uIcaI7VPTv3F1KyD7EF_JZHHLQdErXWRYOPZMTJVws+
'&grant_type='+'refresh_token';

httpReq.setHeader('Content-length',
String.valueOf(messageBody.length()));

httpReq.setBody(messageBody);
try
{
httpRes = http.send(httpReq);
parseJSON(httpRes.getBody());
return true;
}
catch(Exception ex)
{
System.debug('Class: GoogleDrive',
'Method: getTokens',
ex.getMessage());
return false;
}
}
private static void parseJSON(String jsonText)
{
JSONParser parser = null;
parser = JSON.createParser(jsonText);
while (parser.nextToken() != null)
{
if(parser.getText()=='access_token')
{
parser.nextToken();
<Save as Access Token into Key Safe>
}
if(parser.getText()=='refresh_token')
{
parser.nextToken();
<Save as Refresh Token into Key Safe>
}
}
}
}

 

 

Please let me know what is the problem?

bob_buzzardbob_buzzard

Do you get a line number?

 

These lines look suspect to me:

 

'client_id='+107266498774.apps.googleusercontent.com+

and

 

 

'&refresh_token='+1/2uIcaI7VPTv3F1KyD7EF_JZHHLQdErXWRYOPZMTJVws+

 

I reckon the values (right hand side of the equals) need to be enclosed in quotes too, otherwise to the Apex class you are trying to use a local variable whose name starts with 1072 which would be illegal.

 

venchinn2venchinn2

yes it is showing like this

    no viable alternative at character 'a' at line 21 column 28

bob_buzzardbob_buzzard

That is the first line I mentioned - change these to be entire enclosed in quotes and that should fix it.

venchinn2venchinn2

Till i am getting the error will you please edit and send me please?

Rocks_SFDCRocks_SFDC

i think you have copied the code from some where.

 

please type from the keyboard and try it.

 

Thanks,

Anil

Abhi_TripathiAbhi_Tripathi

Hey Venchin,

 

You have taken the that messagebody half in quotes and half was just as it is, controller was unable to understand that unquotes thing , thats qhy it was showing that error

 

Here is you code , successfully saved, i dont know what exaclty you were doing but the error is now resolved

 

public class GoogleDrive {
private static Http http;
private static HttpRequest httpReq;
private static HttpResponse httpRes;

private static void Initialize() {
http = new Http ();
httpReq = new HttpRequest();
httpRes = new HttpResponse();
}

public static boolean getTokens(String tokenType) {

String messageBody='';
Initialize();
httpReq.setMethod('POST');
httpReq.setEndpoint('https://accounts.google.com/o/oauth2/token');
httpReq.setHeader('content-type', 'application/x-www-form-urlencoded');

messageBody = 'client_id=' + '107266498774.apps.googleusercontent.com' + '&client_secret='+ 'Heda08bh6mJLj6d3k5v3HFc2';

if(tokenType=='ALL')

messageBody = messageBody + '&code='+ '4/GT1nC7wEx5nIxR5NLTG84oNOQFvc.ciFUSB7eh3AcXE-sT2ZLcbQhBE8nhQI' +
'&redirect_uri='+ 'https://login.salesforce.com/apex/GoogleDrive' +
'&grant_type=' + 'authorization_code';

else if(tokenType=='ACCESS_TOKEN')

messageBody = messageBody + '&refresh_token='+ '1/2uIcaI7VPTv3F1KyD7EF_JZHHLQdErXWRYOPZMTJVws' +
'&grant_type='+'refresh_token';

httpReq.setHeader('Content-length', String.valueOf(messageBody.length()));
httpReq.setBody(messageBody);

try {

httpRes = http.send(httpReq);
parseJSON(httpRes.getBody());
return true;
}

catch(Exception ex) {

//System.debug('Class: GoogleDrive','Method: getTokens', ex.getMessage());
return false;
}
}

private static void parseJSON(String jsonText) {

JSONParser parser = null;
parser = JSON.createParser(jsonText);
while (parser.nextToken() != null) {

if(parser.getText()=='access_token') {

parser.nextToken();
//<Save as Access Token into Key Safe>
}

if(parser.getText()=='refresh_token') {
parser.nextToken();
// <Save as Refresh Token into Key Safe>
}
}
}
}

venchinn2venchinn2

venchinn2venchinn2
Hi Abhi,
Thank you i am doing like integrate salesforce with google drive will please tell me how it will get and if u give ur mail i will reach me please..