Callback not called? Chrome identity API getProfileUserInfo() - javascript

From the Chrome console window of my Chrome app, I'm making these calls:
window.navigator.appVersion.match(/Chrome\/(.*?) /)[1];
"37.0.2062.120"
chrome.identity.getProfileUserInfo(console.log)
undefined
Why is the callback (console.log) never called? It should print a userInfo object returned w/o needing network IO. I'm getting no callback in the console, or in the code run in my app.
The API is here: https://developer.chrome.com/apps/identity#method-getProfileUserInfo
What did I miss?
Thanks!

console.log is not a JavaScript function. Pass a function (an anonymous function will do) with an argument to the API call, and see if you get something then. Also, this API requires that the manifest.json file have "identity" permission, and (1) you may not have that in the manifest, or (2) you do, but somehow the API call isn't permitted when typing directly to the console (something I personally have never done). If your tests indicate that #2 is a possibility, put the API call into a JavaScript file and try it that way.

Related

Best way to handle chrome.runtime.lastError

as you may all-ready know lastError is a global variable or property from chrome.runtime API used for determining if some error happened during the chrome API call execution.
It is defined only when there was an error, if the API call is OK it won't be defined. The error can be triggered from multiple reasons eg. bad API call due to missing or wrong parameter type, or it can be caused by user interaction, eg. the user rejects permission granting by pressing the "cancel" button.
Chrome has unified it's error handling by using this variable for error reporting across their API's instead of returning an error argument. Due to it's API's async nature Chrome makes an additional check that validates if chrome.runtime.lastError is checked/handled in the callback function from the extension code, if not it throws the famous Unchecked runtime.lastError
You can quickly get the error message by checking chrome.runtime.lastError.message and display it to the user, and all of this is great for most use cases, but I wouldn't write this question if that were my case.
So what happens when you need to implement some additional code logic based on the error result. Let's take launchWebAuthFlow for an example, for the sake of simplicity of the question, I will focus only on the two possible outcomes/errors that can happen:
User interaction required. thrown when API call with bad argument value is made, in this case interactive = false
The user did not approve access. happens when the user closes the authentication window
So my question is, what will be the best way to implement additional logic based on the error?
Is a comparison of chrome.runtime.lastError.message the only way to do it, and if so, is it safe considering that the user browser language might be different than English.
What's your opinion, should Chromium dev team implement: chrome.runtime.lastError.code?

AngularJS Google App Engine API Load Error

When a Google App Engine endpoint API is called immediately once the page is loaded, the following error is thrown. I need to wait for few seconds to call the API.
Then is same when the page is reloaded by some other means, say called from an other page etc.
I have tried adding a callback to call the API after few milliseconds $timeout(queryCallback, 100, false); The results are the same.
Is this a AngularJS error or resource load error? How to solve the error, please share your comments.
angular-1.2.16.min.js:89 TypeError: Cannot read property 'queryInfo' of undefined
at h.$scope.queryInfoAll (controllers.js:641)
at $scope.queryInfo (controllers.js:616)
at angular-1.2.16.min.js:119
at e (angular-1.2.16.min.js:37)
at angular-1.2.16.min.js:40
controllers.js:641
gapi.client.database.queryInfo().execute(function (resp) {
}
In looking at the Java endpoints example here, you use endpoints in javascript by initializing via a call to
<script src="https://apis.google.com/js/client.js?onload=init"></script>
...which calls the JS method named "init", which is a method name of your choosing. This is, however, a plain ole JS method that is unaware of the AngularJS lifecycle. In your init method, it seems you explicitly init your specific endpoint library, as in:
gapi.client.load(apiName, apiVersion, callback, apiRoot);
So, in your "run()", you may have to ask if "gapi" exists and defer until it is. You would then call "gapi.client.load()" with the callback function set.

Mocha conflicting with Nimble (Async)

I'm using nimble.js in my app and mocha + chai for testing, but yesterday I found them to be possibly conflicting.
Basically, when I do a particular http request in my browser, I get
Unauthorized.
which is the correct response.
But using node's http module to do a http request using the same url, I get
not found
Which is confusing me.
I know the http request got the right url because I see it in the server console, even copy pasted it in my browser to be sure.
Additionally, I traced the code to the nimble.parallel function.
I have something like this:
// var _ = require('nimble');
_.parallel(
[
fetch_account(options)
, fetch_invoice(options)
, fetch_site(options)
, fetch_account_stats(options)
]
, render(res, subdomain)
);
// each of the function above returns another function, no simple API gotcha here
In the browser case, an error was correctly identified in a fetch function, then also in the render case.
In the mocha case, an error was correctly identified in a fetch function, but render was not executed. Hence, mocha must've did its own res.render("not found");
Any ideas?
I'm a f*cking idiot.
Forgot to set accept header.
But I'm still confused why I traced to same code but got different behavior.

Must I call sendResponse?

I am using chrome.extension.onRequest.addListener, I see that I am calling sendResponse with no arguments (=> sendResponse(); ), Sometimes I get an error like this:
Error: Attempting to use a disconnected port object
Must I call sendResponse, or I can remove this function if I am not expecting to get a response from the background?
The documentation says:
Function to call (at most once) when you have a response.
This "at most once" sort of indicates that sending a response is optional. While I won't be able to get you an official confirmation, I checked the source code of my extension and there are several messages where sendResponse isn't being called - so far (after a year of heavy use) no issues.

I can't see what went wrong with this WinJS.xhr call

I'm using WinJS.xhr to call a ReST service... when I call the url on the browser I can see the complete returned xml. So I want to parse that xml to show some of the data.
WinJS.xhr({ url: "http://myserver/myservice" }).
then(processPosts, downloadError);
The problem is my downloadError function does not have parameters so I have no idea what went wrong.
What am I missing?
The help page is not very helpful :(
Edit: I used to fiddler to see what's on the wire and I don't see the request. The server I'm targeting is my own LAN, I also tried with its IP Address with same results (none)
When there is an error the callback function will take one parameter. The downloadError will need to take in one parameter. If you define downloadError as follows you should get more details. The result type should be XMLHttpRequest and using that you can see the status of the request and why it failed.
function downloadError(result){
//check the result param.
}
EDIT:
Check the app capabilities in your application.AppManifest file. The capabilities section is where you define what capabilities are required by your app for example connect to the internet, use the webcam.

Categories