Gmail API 403 Insufficient Permissions domain global - android

#Override
protected List< String > doInBackground( Void... params )
{
try
{
//This line below is the cause of the insufficient permissions error
ListMessagesResponse messagesWithLabels = mService.users().messages().list("me").setQ("label:inbox").execute();
/*for( Message m : messagesWithLabels.getMessages( ) )
{
if( m.size( ) > MAXSIZE )
{
List<MessagePartHeader> headers = m.getPayload( ).getHeaders( );
for ( MessagePartHeader header : headers )
{
if ( header.getName( ).equals("From") || header.getName().equals("Date")
|| header.getName().equals("Subject") || header.getName().equals("To")
|| header.getName().equals("CC")) {
messageDetails.put( header.getName( ).toLowerCase( ), header.getValue( ) );
}
}
messageDetails.put("snippet", m.getSnippet());
messageDetails.put("threadId", m.getThreadId());
messageDetails.put("id", m.getId());
messageDetails.put("body",m.getPayload().getBody().getData());
GmailFunctions.deleteThread( mService, "me", m.getId( ) );
messageDetails.clear( );
}
}*/
return getDataFromApi( );
}
catch ( Exception e )
{
mLastError = e;
cancel( true );
return null;
}
}
I have marked the line which is causing a 402 Insufficient permissions domain: global error. If I comment out said line the program will return the labels and print them to the screen without the permissions error. I have signed my release apk and set up the Google Play Developer console. The app is signing just fine it's SHA1 and I followed the sample application which retrieves credentials.
https://developers.google.com/gmail/api/quickstart/java
What to do about insufficient permissions?
Thank you.
The creation of mservice:
private class MakeRequestTask extends AsyncTask< Void, Void, List< String > > {
private com.google.api.services.gmail.Gmail mService = null;
private Exception mLastError = null;
ArrayList<String> sRemovalIds = new ArrayList<String>( );
List< String > inserts = new ArrayList< String >( );
Map<String, Object> messageDetails = new HashMap<String, Object>( );
public MakeRequestTask( GoogleAccountCredential credential )
{
HttpTransport transport = AndroidHttp.newCompatibleTransport( );
JsonFactory jsonFactory = JacksonFactory.getDefaultInstance( );
mService = new com.google.api.services.gmail.Gmail.Builder(
transport, jsonFactory, credential )
.setApplicationName( "Gmail API Android Quickstart" )
.build( );
}
#Override
protected List< String > doInBackground( Void... params )
{
try
{
ListMessagesResponse messagesWithLabels = mService.users().messages().list("me").setQ("label:inbox").execute();
/*for( Message m : messagesWithLabels.getMessages( ) )
{
if( m.size( ) > MAXSIZE )

private static final String[ ] SCOPES = { GmailScopes.GMAIL_LABELS, GmailScopes.GMAIL_COMPOSE,
GmailScopes.GMAIL_INSERT, GmailScopes.GMAIL_MODIFY, GmailScopes.GMAIL_READONLY, GmailScopes.MAIL_GOOGLE_COM };
Using these scopes instead of the default with only GMAIL_LABELS worked for me.

You may also need to delete your previous credentials file after changing permissions. This is usually in $HOME/.credentials/

You may also need to delete your previous credentials file after changing permissions. It's token.json in the current folder.

Apart from setting scopes mentioned by #Eae, delete StoredCredential file, path of file is your_project/tokens/StoredCredential.

Related

APIs won't load when published on Google Playstore

So this is my first time publishing a Flutter app to the Google Play Store. I wanted to create something simple so I could learn the entire process. So I created a simple Trivia App.
When I run the app from my phone or emulator (Android or iPhone) the free API that I'm using here loads the categories without any issues.
The issue I'm having is when I publish the app to the Play Store, the API doesn't load and I don't even get an error message. Just a blank screen.
Here is the API service call in my app:
static Future<List<Category>> fetchCategories() async {
const url = "https://opentdb.com/api_category.php";
var response;
try {
response = await http.get(url);
if (response.statusCode == 200) {
var jsonString = response.body;
final Map<String, dynamic> responseData = json.decode(jsonString);
var list = responseData['trivia_categories'] as List;
var _items = list.map((i) => Category.fromJson(i)).toList();
return _items;
} else {
return null;
}
} on Exception {
throw response.statusCode;
}
}
And this is the code in the controller that calls the API's method.
void fetchCategories() async {
// flag as loading.
isLoading(true);
try {
//get categories from API server
var categories = await ApiServices.fetchCategories();
if (categories != null) {
categories.forEach((Category categoryItem) async {
// adjust data accordingly
categoryItem.totalQuestions =
await fetchCategoryCount(categoryItem.id);
if (userDataController.data.read(categoryItem.id.toString()) !=
null) {
// get a list of answered questions from the device and count them
List<dynamic> correctAnswers =
userDataController.data.read(categoryItem.id.toString());
categoryItem.correctAnswers = correctAnswers.length;
} else {
categoryItem.correctAnswers = 0;
}
categoryTiles.add(categoryItem);
});
}
} on Exception catch (e) {
throw new Exception("An error occured fetching the data");
} finally {
isLoading(false);
}
}
Has anyone else ran into this issue?

Why can't I connect to an API when I generate the APK?

I have a problem with a login that I am doing, in my emulator it works correctly but when I generate the apk and test it on the phone, the API does not work.
I already assigned the Internet permission in the .xml and made tests to use Internet images and everything fine, so I discarded the permissions.
I do not know if it gives me an error because I use an http and not an https, or if someone has an idea of ​​what is happening here they tell me, I attach my code
Code:
void _login(BuildContext context) async {
if (!_loading) {
setState(() {
_loading = true;
});
}
//print(_username.value.text);
//print(_password.value.text);
var url = Uri.parse(
"http://taquillamedicina.mdgsystem.com/index.php?route=api/loginapp");
String var1 = 'vipmedico';
String var2 =
'xxxxx';
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
Map data = {
'username': var1,
'password': var2,
'usernameapp': _username.value.text,
'passwordapp': _password.value.text
};
var jsonResponse = null;
var response = await http.post(url, body: data);
//print(response);
if (response.statusCode == 200) {
jsonResponse = json.decode(response.body);
// print(jsonResponse);
if (jsonResponse != null) {
setState(() {
_loading = false;
});
sharedPreferences.setString("client_id", jsonResponse['client_id']);
if (jsonResponse['error'] == '1') {
Navigator.of(context).pushReplacementNamed("/list");
}
}
} else {
setState(() {
_loading = false;
});
print(response.body);
}
}
To get over this, you will have add android:usesCleartextTraffic="true" to your AndroidManifest.XML file. But as advised, this isn't solving th eproblem, it's sweeping it under the carpet, but will get your task done for now.
After I upgrade the Flutter 2.0 my app is down and I add under Anroid>Src>Main>AndroidManifest.xml
android:usesCleartextTraffic="true"
API and app is worked

Ssocket exception in AWS AppSync in flutter

I am working on flutter application with AWS AppSync in that I am using the end URL of AWS server but when I try to execute the query I am getting below error
I am working in proxy setting but is not effecting this URL it's working perfectly in native android application
SocketException: OS Error: Connection refused, errno = 111, address = XXXXXXXXXXXXX.appsync-api.ap-south-1.amazonaws.com port -43872
I have a search on google but they mention the internet permission in not added in the manifest file but after added the permission I am facing the same issue.
Below is the code on the AWS app sync execute method where I am getting the error.
Future<Map> execute({
#required String endpoint,
#required String query,
#required Map variables,
#required String accessToken,
Database cache,
CachePriority priority = CachePriority.network,
}) async {
var body = jsonEncode({"query": query, "variables": variables});
Future<Map> loadFromCache() async {
var cacheKey = getCacheKey(endpoint, body);
var data = await readCache(cache, cacheKey);
if (data != null) {
logger.fine(
'loaded from cache (endpoint: ${endpoint.toRepr()}, requestBody: ${body.toRepr()}, cacheKey: $cacheKey)',
);
}
return data;
}
if (cache != null && priority == CachePriority.cache) {
var data = await loadFromCache();
if (data != null) return data;
}
logger.fine('POST ${endpoint.toRepr()} - ${body.toRepr()}');
http.Response response;
try {
response = await http.post(
endpoint,
headers: {
HttpHeaders.authorizationHeader: AWSaccessToken,
HttpHeaders.contentTypeHeader: ContentType.json.mimeType,
"x-api-key" :AWS_APP_SYNC_KEY,
},
body: body,
);
} catch (e) {
var shouldFallback = cache != null && priority == CachePriority.network;
if (!shouldFallback || !isNetworkError(e)) rethrow;
logger.finest('network error encountered; falling back to cache - $e');
var data = await loadFromCache();
if (data != null) {
return data;
} else {
rethrow;
}
}
if (response.statusCode != HttpStatus.ok) {
throw HttpError(response);
}
logger.fine(
'loaded from network (endpoint: ${endpoint.toRepr()}, requestBody: ${body.toRepr()})',
);
var result = jsonDecode(response.body);
var data = result["data"];
if (cache != null) {
var cacheKey = getCacheKey(endpoint, body);
await updateCache(cache, cacheKey, data);
logger.fine(
'updated cache (endpoint: ${endpoint.toRepr()}, requestBody: ${body.toRepr()}, cacheKey: $cacheKey)',
);
}
return data;
}
you are connecting to the wrong port, I mean the port you are connecting to from your client has no listener/server, or the port and public IP is not exposed to the internet on your server side

Why does Google ReCaptcha API allow bad secret key and/or bad user token response?

Take a look at the code below. This is server-side. I call the Google ReCaptcha API with bad secret key and bad user token response, as you can see. You know what? It works! More precisely: Google ReCaptcha API doesn't answer with exception (i.e.: my catch isn't reached). Why? This is not the expected behavior, right?
/**
* Verifies a Recaptcha filled by the user in his Android app.
* 1. Success: returns the JSON response
* 2. Failure: throws the error
**/
exports.verifyRecaptcha = functions.https.onCall((data, context) => {
const user_response_token = data.userResponseToken;
if(user_response_token === null || user_response_token === '') {
throw new functions.https.HttpsError('invalid-argument', 'The function must be called with an adequat user response token.');
}
const remote_url = 'https://recaptcha.google.com/recaptcha/api/siteverify';
const secret = '<MY_REAL_SECRET_KEY>'; // Original value: 'https://www.google.com/recaptcha/api/siteverify'; # Moises' value: https://recaptcha.google.com/recaptcha/api/siteverify
var options = {
method: 'POST',
uri: remote_url,
body: {secret: 'Foo', response: 'Bar'},
// body: {secret: secret, response: user_response_token},
json: true
};
return rp(options)
.then(parsedBody => {
return {code: 'Success', message: 'You are actually a human (this msg is for test purposes).'};
})
.catch(error => {
throw new functions.https.HttpsError('unknown', error);
});
});
And below is the Android app code:
final SafetyNetApi.RecaptchaTokenResponse response = task.getResult();
assert response != null;
final String userResponseToken = response.getTokenResult();
if (!userResponseToken.isEmpty()) {
final HashMap<String, String> the_data = new HashMap<>();
the_data.put("userResponseToken", userResponseToken);
FirebaseFunctions.getInstance()
.getHttpsCallable("verifyRecaptcha")
.call(the_data)
.continueWith(new Continuation<HttpsCallableResult, Void>() {
#Override
public Void then(#NonNull final Task<HttpsCallableResult> task) {
if(context.isDestroyed() || context.isFinishing()) {
return null;
}
if(!task.isSuccessful()) {
Exception e = task.getException();
if (e instanceof FirebaseFunctionsException) {
FirebaseFunctionsException ffe = (FirebaseFunctionsException) e;
System.out.println(ffe.getMessage());
}
return null;
}
callback.onAsking();
return null;
}
});
} else {
callback.onFailureUserResponseTokenIsEmpty();
}
The docs suggest that errors such as invalid-input-secret/invalid-input-response will appear in the error-codes field of the response.
This information doesn't necessarily need to be translated into an HTTP error code (which would cause your catch block to execute); in this instance, Google apparently wanted to support multiple simultaneous error messages, and the HTTP response code pertains more to the conduct of the protocol at the HTTP level.
While we're looking at the docs, I should point out that you probably want to refer to the success field before presuming that your user is a human.

AWS Lambda : errorMessage Process exited before completing request

Hi I'm newbie in android!
I want to upload image file from android client to server(Server makes thumbnail, and return thumbnail's url).
However I stucked in this error message.
{"errorMessage":"RequestId: 8e2a21b8-e62e-11e8-8585-d9b6fdfec9b9 Process exited before completing request"}!
I tried to find this error code in stackoverflow, but i cannot found answer for android.
Please help or give me link where I can solve this problem...
Here is server code.
const AWS = require('aws-sdk');
const multipart = require("parse-multipart");
const s3 = new AWS.S3();
const bluebird = require('bluebird');
exports.handler = function(event, context) {
let result = [];
const bodyBuffer = new Buffer(event['body-json'].toString(), 'base64');
const boundary = multipart.getBoundary(event.params.header['Content-Type']);
const parts = multipart.Parse(bodyBuffer, boundary);
const files = getFiles(parts);
return bluebird.map(files, file => {
console.log('UploadCall');
return upload(file)
.then(
data => {
result.push({
'bucket': data.Bucket,
'key': data.key,
'fileUrl': file.uploadFile.fullPath })
console.log( `DATA => ${JSON.stringify(data, null, 2 )}`);
},
err => {
console.log(`S3 UPLOAD ERR => ${err}`);
}
)
})
.then(_=> {
return context.succeed(result);
});
}
let upload = function(file) {
console.log('PutObject Call')
return s3.upload(file.params).promise();
};
let getFiles = function(parts) {
let files = [];
parts.forEach(part => {
const buffer = part.data
const fileName = part.filename;
const fileFullName = fileName;
const originBucket = 'dna-edge/images';
const filefullPath = `https://s3.ap-northeast-2.amazonaws.com/${originBucket}/${fileFullName}`;
const params = {
Bucket: originBucket,
Key: fileFullName,
Body: buffer
};
const uploadFile = {
size: buffer.toString('ascii').length,
type: part.type,
name: fileName,
fullPath: filefullPath
};
files.push({ params, uploadFile })
});
return files;
};
And this is client code.(imgURL looks like /storage/emulated/0/DCIM/img/1493742568136.jpg)
public static String requestHttpPostLambda(String url, String imgURL){
/*
await axios.post(`${AWS_LAMBDA_API_URL}?type=${type}`, formData,
{ headers: { 'Content-Type': 'multipart/form-data' }})
.then((response) => {result = response});
*/
String result=null;
try {
HttpClient client = new DefaultHttpClient();
String postURL = url;
HttpPost post = new HttpPost(postURL);
post.setHeader("Content-Type", "multipart/form-data");
File file = new File(imgURL);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
builder.addPart("image", new FileBody(file));
post.setEntity(builder.build());
HttpResponse responsePOST = client.execute(post);
Log.e("HttpResponse", responsePOST.getStatusLine()+"");
HttpEntity resEntity = responsePOST.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
Welcome to stackoverflow.
So for some reason AWS aren't too good an updating the docs, don't use context.succeed, use the callback thats passed as a third param.
Also I'd move to Node 8.10 runtime because then rather than using promises/then pattern you can use async/await.
export default(event, context, callback) => {
try {
// do some stuff
callback(null, SOME_VALID_HTTP_RESPONSE)
} catch(e){
callback(e, null)
}
}
There's a few reason your Lambda could be failing, if the process exited before completing it's either crashing OR you're not returning a valid HTTP response(if your lambda is behind API gateway)
Two solutions - first place to look is in cloudwatch, find your lambda function name and check the latest log to look for error messages.
Second - check out my answer here so when your function succeeds you need to return a valid HTTP response to API Gateway so in essence if you use my code from there you can do:
callback(null, responder.success({someJson: someValue}))
Any questions let me know :-)
EDIT: I'm updating this question I'm just working on an example for a multiple file upload to S3!

Categories

Resources