Android Management API change Policy for device - android

I created 2 policies in my enterprise.
I would like to switch one device to the other policy without re-enroling it.
I tried to use android management enterprises devices.patch with following JSON
{ "policyName": "policy2" }
this link
When i execute this command i always get follwoing error Message:
{
"error": {
"code": 400,
"message": "Illegal state transition from ACTIVE to DEVICE_STATE_UNSPECIFIED",
"status": "INVALID_ARGUMENT"
}
}
Does anybody know how to change policy for a device without wiping it?

It is indeed possible to change the policy of a device without re-enrolling it, and you're not far from the solution.
You get this error because you implicitly attempt to change other fields of the Device resource (in particular the state field) by not setting them in the resource that you send in devices.patch.
You have two options:
Set the updateMask in devices.patch to "policyName", to tell the API that you only want to change the policyName field.
Call devices.get to get the current Device resource, and then send back the entire resource with only the policyName field changed in to devices.patch.
Using updateMask is preferable because it does an atomic read-modify-write.

As Fred mentioned, updateMask is the preferred way to go. Here's an example of how to use the updateMask approach in the Google colab
swap_result = androidmanagement.enterprises().devices().patch(
name='enterprises/ENTERPRISE_NAME/devices/DEVICE_ID', updateMask='policyName', body={ "policyName": "enterprises/ENTERPRISE_NAME/policies/NEW_POLICY_NAME"}).execute()

i foudn a solution for my problem:
{ "policyName": "policy2" , "state":"active"}

If you try this from management api test site then add updateMask as "policyName"

Related

Branch.io deferred deep link missing data after install

I'm trying to get deferred deep links working on Android. I added a test device in the dashboard & reset the device data and made sure the app is uninstalled. When I click a branch.io link, it takes me to the play store as expected. I then launch the app from Android Studio onto the phone, then the logs say that Branch SDK sent a request to https://api2.branch.io/v1/install, but the problem is the response doesn't contain the original link, query params, or key value pairs I set in the dashboard. This is the JSONObject I'm receiving in onInitFinished:
{"+match_guaranteed":true,"link_click_id":"976953359423305632","+clicked_branch_link":true,"+click_timestamp":1634196720,"+is_first_session":true,"utm_source":"test-referrer"}
Where is all the other information? This doesn't include the original link, the key-value pairs, tags, etc.
For a comparison, this is what I receive in the iOS app:
["$ios_passive_deepview": "branch_passive_default", "source": "test-referrer", "+is_first_session": 1, "~channel": test-referrer, "$matching_ttl_s": 7200, "~id": 976763609660742721, "~creation_source": 1, "$one_time_use": 0, "~marketing": 1, "~referring_link": "https://myapp.test-app.link/test-referrer", "~feature": "test", "+click_timestamp": 1634249299, "+match_guaranteed": 0, "$og_description": "My app description", "$og_title": "MyApp", "+clicked_branch_link": 1, "$marketing_title": "Test Referral Link", "~tags": ["test-referrer"], "~campaign": "test"]
If I rotate the phone to recreate the Activity or reopen the app a single time, it then sends a request to https://api2.branch.io/v1/open and returns all the info I expected initially. How do I get the information after installing the app?
I'm currently testing with myapp.test-app.link, and I call Branch.enableTestMode() before Branch.getAutoInstance(this) in my custom Application class's onCreate(). I also tried with a live link and got the same result.
These are the libraries I'm using in build.grade:
implementation 'io.branch.sdk.android:library:5.0.13'
implementation 'com.google.firebase:firebase-appindexing:20.0.0'
implementation 'com.google.android.gms:play-services-ads-identifier:17.1.0'
implementation 'androidx.browser:browser:1.3.0'
I've also set up app links and the uri scheme in the dashboard as well as in the app. Using getFirstReferringParams() and getLatestReferringParams() on the first session after installing doesn't help either.
UPDATE:
Repeating the exact same testing process I described above, now the JSONObject that gets passed into onInitFinished has even less information and is claiming that I'm not clicking a branch link:
{"+clicked_branch_link":false,"+is_first_session":true}
And getFirstReferringParams() returns an empty json object.
I'm about to start looking for an alternative at this rate.
I fixed this issue by adding intent.putExtra("branch_force_new_session", true); right after this.activity.setIntent(intent); when initialising the intent. Something like this:
...
this.activity.setIntent(intent);
intent.putExtra("branch_force_new_session", true);
Branch
.sessionBuilder(this.activity)
.withCallback(branchReferralInitListener)
.reInit();
...
Look here for the branch_force_new_session reference.
The other reason for the missing data is the non-existent link alias, but it looks like it is not the case as soon as you tried with the same link in iOS.

Deleting a user from Azure Active Directory B2C Android/Java

I have an Android application in which I'm using Azure AD B2C to authenticate users. Users login and logout of the application as needed. I would like to give the user the option to delete their own account.
I understand that I need to use the Azure AD Graph API to delete the user. This is what I have so far:
According to this link, it looks like deleting a user from a personal account (which is what the B2C users are using) is not possible. Is that correct?
Here's my code snippet for the Graph API call. Feel free to ignore it if I'm off track and there is a better way to solve this.
I believe I need a separate access token than what my app currently has (as the graph API requires other API consent). So, I'm getting the access token as follows:
AcquireTokenParameters parameters = new AcquireTokenParameters.Builder()
.startAuthorizationFromActivity(getActivity())
.fromAuthority(B2CConfiguration.getAuthorityFromPolicyName(B2CConfiguration.Policies.get("SignUpSignIn")))
.withScopes(B2CConfiguration.getGraphAPIScopes())
.withPrompt(Prompt.CONSENT)
.withCallback(getGraphAPIAuthCallback())
.build();
taxApp.acquireToken(parameters);
In the getGraphAPIAuthCallback() method, I'm calling the Graph API using a separate thread (in the background):
boolean resp = new DeleteUser().execute(authenticationResult.getAccessToken()).get();
Finally, in my DeleterUser() AsyncTask, I'm doing the following:
#Override
protected Boolean doInBackground(String... aToken) {
final String asToken = aToken[0];
//this method will be running on background thread so don't update UI from here
//do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
IAuthenticationProvider mAuthenticationProvider = new IAuthenticationProvider() {
#Override
public void authenticateRequest(final IHttpRequest request) {
request.addHeader("Authorization",
"Bearer " + asToken);
}
};
final IClientConfig mClientConfig = DefaultClientConfig
.createWithAuthenticationProvider(mAuthenticationProvider);
final IGraphServiceClient graphClient = new GraphServiceClient.Builder()
.fromConfig(mClientConfig)
.buildClient();
try {
graphClient.getMe().buildRequest().delete();
} catch (Exception e) {
Log.d(AccountSettingFragment.class.toString(), "Error deleting user. Error Details: " + e.getStackTrace());
}
return true;
}
Currently, my app fails when trying to get an access token with a null pointer exception:
com.microsoft.identity.client.exception.MsalClientException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
Any idea what I need to do to provide the user the option to users to delete their own account? Thank you!
Thanks for the help, #allen-wu. Due to his help, this azure feedback request and this azure doc, I was able to figure out how to get and delete users silently (without needing intervention).
As #allen-wu stated, you cannot have a user delete itself. So, I decided to have the mobile app call my server-side NodeJS API when the user clicks the 'Delete Account' button (as I do not want to store the client secret in the android app) and have the NodeJS API call the Azure AD endpoint to delete the user silently. The one caveat is that admin consent is needed the first time you try to auth. Also, I have only tested this for Graph API. I'm not a 100% sure if it works for other APIs as well.
Here are the steps:
Create your application in your AAD B2C tenant. Create a client secret and give it the following API permissions: Directory.ReadWrite.All ;
AuditLog.Read.All (I'm not a 100% sure if we need the AuditLog permission. I haven't tested without it yet).
In a browser, paste the following link:
GET https://login.microsoftonline.com/{tenant}/adminconsent?
client_id=6731de76-14a6-49ae-97bc-6eba6914391e
&state=12345
&redirect_uri=http://localhost/myapp/permissions
Login using an existing admin account and provide the consent to the app.
Once you've given admin consent, you do not have to repeat steps 1-3 again. Next, make the following call to get an access token:
POST https://login.microsoftonline.com/{B2c_tenant_name}.onmicrosoft.com/oauth2/v2.0/token
In the body, include your client_id, client_secret, grant_type (the value for which should be client_credentials) and scope (value should be 'https://graph.microsoft.com/.default')
Finally, you can call the Graph API to manage your users, including deleting them:
DELETE https://graph.microsoft.com/v1.0/users/{upn}
Don't forget to include the access token in the header. I noticed that in Postman, the graph api had a bug and returned an error if I include the word 'Bearer' at the start of the Authorization header. Try without it and it works. I haven't tried it in my NodeJS API yet, so, can't comment on it so far.
#allen-wu also suggested using the ROPC flow, which I have not tried yet, so, cannot compare the two approaches.
I hope this helps!
There is a line of code: graphClient.getUsers("").buildRequest().delete();
It seems that you didn't put the user object id in it.
However, we can ignore this problem because Microsoft Graph doesn't allow a user to delete itself.
Here is the error when I try to do it.
{
"error": {
"code": "Request_BadRequest",
"message": "The principal performing this request cannot delete itself.",
"innerError": {
"request-id": "8f44118f-0e49-431f-a0a0-80bdd954a7f0",
"date": "2020-06-04T06:41:14"
}
}
}

Codename one GoogleConnect setScope is not effective

I am using the new GoogleConnect thing and set the scope like
Login gc = GoogleConnect.getInstance();
gc.setScope("https://www.googleapis.com/auth/plus.profile.emails.read https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file");
...
gc.doLogin();
However, when the access token is generated and checked in
https://www.googleapis.com/oauth2/v1/tokeninfo?access_token=(accesstoken)
, it shows
"scope": "https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile"
Is it related to any recent updates or new build hint?
I am totally lost here. It is an Android build.
Thanks.
Edited to add gc.doLogin() code, to clarify that I am NOT trying to set the scope after the access token is generated.
Same problem here, setScope only works on the simulator
"scope": "https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/userinfo.profile",
My scopes (for reference):
google.auth.scopes=https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/plus.me email profile

Nest ETA update not working: No write permission(s) for field(s): eta

I'm building an Application that integrates with your Nest devices (both the thermostat and the Nest Protect, but this issue is about the thermostat).
What I'm trying to do is set my thermostat's ETA to be in x minutes (2 hours for example so 120 minutes).
This is my code that I'm executing:
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSSZ");
final String path = buildStructureFieldPath(structureID, Keys.STRUCTURE.ETA);
Structure.ETA eta = new Structure.ETA.Builder()
.setTripID(tripId)
.setEstimatedArrivalWindowBegin(sdf.format(estimatedArrivalBegin))
.setEstimatedArrivalWindowEnd(sdf.format(estimatedArrivalEnd))
.build();
sendRequest(path, eta.toJSON().toString(), listener);
The path is /structures/MY_STRUCTURE_ID/eta
Unfortunately that's not working. I'm always getting an error code -2 and error message: No write permission(s) for field(s): eta
And that's were it gets strange. No permission, but I did request the permission and I did an authenticate, which is successful, before launching the update call.
In the two attached screenshots you can see first my Nest Developer Account where you can find the ETA write permission and in the second you can see the logging from within my app (using the NestAPI as can be found on GitHub, just added the ETA feature myself).
Anyone have any idea on how to solve this issue?
Can you print out the exact JSON blob you're sending and post it here? (the value of eta.toJSON().toString())
Best guess is that it isn't formatted exactly correctly and as such is maybe attempting to write in such a way that doesn't adhere to the api-reference.
This is the format that it needs to match:
"eta": {
"trip_id": "myTripHome1024" ,
"estimated_arrival_window_begin": "2015-10-31T22:42:59.000Z" ,
"estimated_arrival_window_end": "2015-10-31T23:59:59.000Z"
}
Single line:
{"eta":{"trip_id":"myTripHome1024","estimated_arrival_window_begin":"2015-10-31T22:42:59.000Z","estimated_arrival_window_end": "2015-10-31T23:59:59.000Z"}}
To pinpoint exactly which field may be erroneous, try sending just one change at a time for each ie: structures/ID/eta/trip_id, etc for the others.
Useful JSON Validator: http://jsonlint.com/
You could also try to send it to /structures/MY_STRUCTURE_ID.json?auth=[TOKEN] instead of /structures/MY_STRUCTURE_ID/eta.

Appium: "An element could not be located on the page using the given search parameters" error

I am new to Appium and have been trying to automate the Conversion Calculator app for Android. Am getting the error "org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters", when trying to find a EditText element. Using Appium ver 1.0.0 and Android 4.3
The following is my code:
List<WebElement> textViews = driver.findElements(By.className("android.widget.TextView"));
for (i=0; i<textViews.size(); i++) {
if(textViews.get(i).getText().toLowerCase().contains("memory")) {
textViews.get(i).click();
}
}
Thread.sleep(5000);
WebElement editText = driver.findElement(By.className("android.widget.EditText"));
editText.sendKeys("123");
Even findElement by ID is not working. Please let me know what I am doing wrong here or if I need to provide more details.
I would use
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); instead of Thread.sleep(5000).
Try to use a newer version of Appium, I's been improved a lot. You can download the latest version of Appium and Appium clients here:http://appium.io/downloads.html
But be careful because in the newer version the findElement throws an Exception if there are more then one result of the search.
I would write this in a comment but I've not enough reputation :/
Possible Cause:
Multiple EditText in the current screen.
Please try with the following:
Solution1:
List<WebElement> editText = driver.findElements(By.className("android.widget.EditText"));
editText.get(0).sendKeys("123");
0 - Index of EditText
Solution2:
Use any other locating strategy like Xpath.
Maybe you could try waiting until the element is visible or enabled using a WebDriverWait object?
Avoid using sleep as much as possible, try using the WAIT command.
Sleep without waiting for the time that has been determined, even if the element is already on the screen.
In the case of the wait command, as soon as the element appears, the action will already be performed, this along the code will reduce the execution time considerably.
The issue for me was the app path I was using. If you are using a config file make sure to declare the application separately from the device.
If not, make sure the "app" capability has the right path. Here is the code in my config file for example:
devices_by_ids = {
"platformName": "Android",
"appium:DEVICE ADB ID": {
"android_version": "13",
"device_name": "google_Pixel_5a",
"DEVICE ADB ID": "DEVICE ADB ID",
"port":"4723",
"autoGrantPermissions": "true",
},
"appium:app": "YOUR APP PATH",
"appium:appWaitActivity": "*"
}
In Appium v2 use
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

Categories

Resources