Event Sync Token (412 response status ) - asana

I'm using RestTemplate to make the GET call to asana's REST Api.
By using postmen when i'm calling:
https://app.asana.com/api/1.0/events?resource=PROJECT_ID
I'm getting a message and a sync token ( this is the same case when the sync token is too old and needs to be renewed ).
By using the RestTemplate, when the sync token is too old \ its the first call i'm making and I need a sync token, I'm getting a 412 response "Prediction Faild".
This happens also in postman, but i'm getting along with the "error" message the new sync token.
With the RestTemplate all i'm getting is this error:
Aug 06, 2015 3:56:55 PM org.springframework.web.client.RestTemplate handleResponseError
WARNING: GET request foPROJECT_ID21650756795165" resulted in 412 (Precondition Failed); invoking error handler
Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 412 Precondition Failed
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:90)
at org.springframework.web.client.RestTemplate.handleResponseError(RestTemplate.java:494)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:451)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:409)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:385)
at availo.worker.asana.MainTask.getEvents(MainTask.java:86)
at availo.worker.asana.MainTask.getProjects(MainTask.java:76)
at availo.worker.asana.MainTask.main(MainTask.java:115)
Any suggestions?
Thanks!

This worked for me. It lets you read the message that comes with the error:
restTemplate.setErrorHandler(new DefaultResponseErrorHandler(){
protected boolean hasError(HttpStatus statusCode) {
return false;
}});

When first subscribing to Events on a resource you will receive a 412 Precondition Failed response code due to the fact that there is no sync token established yet. You should extract that sync token and use it in your next request to begin receiving events.
It looks like RestTemplate is invoking an error handler due to the 412 response code, which is understandable as a 412 is an error code.
If you can override DefaultResponseErrorHandler.handleError(), check that the response is a 412, then extract the sync token you could pass that in your next request to get events on the resource.
Another option is to use our Java client library, which should handle all of this for you.
This is what it might look like using our client:
System.out.println("Watching for events on project: " + project.name);
for (Event event : client.events.get(project.id)) {
System.out.println("User: " + event.user.name + "\nAction: " + event.action + "\nResource: " + event.resource);
}

Related

GET request fails on electron but works when I run the URL which failed on the browser

I'm currently trying to get the authorization token from Okta using a GET request from my app using fetch API. The first step works well and I get the sessionToken. The next step requires me to pass this sessionToken in the url of a get request . Once this is done I should be getting an html object but what I instead get is a 404 message from the server on the console and the id_token embedded in the failed url which also is on the console. I have tried every possible fix and yet it doesn't seem to work.
async function getAccessToken(url = '', sessionID= ''){
const response = await fetch(url + sessionID);
return response
}
ON THE CONSOLE: Failed to load resource: the server responded with a status of 404 () the failed url with the id_token
When I copy this URL and run it in the browser it works. Response contains the callback url alone and I don't seem to have anyway to access the id_toke. Any way to access this id_ token would do for now.

Dart HttpClient with Basic Authentication issues after flutter upgrade

After a lot of browsing and reading docs, I came across this answer, which worked until I upgraded flutter.
My current issue is that the credentials do not reach my server.
IntelliJ error:
I/FlutterActivityDelegate( 6944): onResume setting current activity to this
D/EGL_emulation( 6944): eglMakeCurrent: 0xa7f852a0: ver 2 0 (tinfo 0xa7f83250)
I/flutter ( 6944): doing login with credentials:: W and T
I/flutter ( 6944): my_response is:: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
I/flutter ( 6944): <title>429 Too Many Requests</title>
I/flutter ( 6944): <h1>Too Many Requests</h1>
I/flutter ( 6944): <p>1 per 1 minute</p>
E/flutter ( 6944): [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 6944): type '(Exception) => void' is not a subtype of type '(Object) => FutureOr<dynamic>'
My server output for the request is:
credentials:: -
192.168.1.175 - - [12/May/2018 10:56:23] "GET /users/login HTTP/1.1" 401 -
192.168.1.175 - - [12/May/2018 10:56:23] "GET /users/login HTTP/1.1" 429 -
LE: the "credentials:: - " is a print done in the authentication level of the server.
LLE: a curl request is working just fine
curl -u 123456:password http://localhost:5000/users/login
{
"is_logged_in": true,
"last_login": 1525980360
}
Response is:
credentials:: 123456 - password
127.0.0.1 - - [12/May/2018 13:00:50] "GET /users/login HTTP/1.1" 200 -
I am using the exact same code as in the link provided above.
Here's a summary answer wrapping up the discussion in the comments.
It seems like the Too Many Requests error may have been triggered by the typical behaviour of HTTP client talking to server that requires authorization.
GET ->
<- 401 Unauthorized + scheme + realm (and nonce if applicable)
GET with Authorization header ->
<- 200 OK (if credentials are valid)
It seems like the server may have counted the first GET/401 towards some sort of limit of requests per minute.
However, a request from curl was successful, probably because curl preemptively sent the Authorization header with the first GET, instead of waiting to be asked with a 401. It can do this as long as the authorization scheme is Basic because it doesn't need any information from the server. (It wouldn't work with Digest because it cannot calculate the Authorization header without the nonce sent with the 401.)
So the question arises, is there a way to preemptively send Basic auth in package:http?
Normal way - only send in response to a 401
// create an IOClient with authentication
HttpClient inner = new HttpClient();
inner.authenticate = (uri, scheme, realm) {
inner.addCredentials(
uri, realm, new HttpClientBasicCredentials(username, password));
};
http.IOClient client = new http.IOClient(inner);
// and use it like this
http.Response response = await client.get('https://api.somewhere.io');
// todo - handle the response
Preemptive way - only works with Basic authentication
// use the normal http.get method, but add the header manually, with every request
http.Response response = await http.get(
'https://api.somewhere.io',
headers: {'authorization': basicAuthenticationHeader(username, password)},
);
// todo - handle the response
with the utility method
String basicAuthenticationHeader(String username, String password) {
return 'Basic ' + base64Encode(utf8.encode('$username:$password'));
}

Response code 411 mapped as failure

when Im trying to run my flow, getting this
error Response code 411 mapped as failure Response code 411 mapped as failure.
Payload : org.glassfish.grizzly.utils.BufferInputStream#16e8c87
Element : /post_sampleFlow/processors/1 # post_sample:post_sample.xml:70 (HTTP_Post).
please tell me what is the problem.
So problem is how to use mule to auth user.
From this doc:
<set-property propertyName="Authorization" doc:name="Set Authentication Header" value="#["Basic " + Base64.encodeBase64String("username:password")]"/>
So, that way all properties can be filled.
BTW now I see need for that flows.

Bad request on ProcessUserAuthorization (DotNetOpenAuth 4.2.2.13055)

I'm trying to upgrade the DotNetOpenAuth verson to 4.2.2.13055, in the Google dotnet client library.
So I downloaded the latest dlls - DotNetOpenAuth.Core, DotNetOpenAuth.OAuth2, etc. (we still don't work with NuGet).
I made a small change (changed the way I construct NativeApplcationClient with client_id and client_secret) to support the new version.
Then, I tried to run a sample we have in our samples repository (e.g. https://code.google.com/p/google-api-dotnet-client/source/browse/Tasks.SimpleOAuth2/Program.cs?repo=samples), and I got a bad request error, as following:
DotNetOpenAuth.Messaging.ProtocolException: Error occurred while sending a direct message or getting the response. --->
System.Net.WebException: The remote server returned an error: (400) Bad Request.
at System.Net.HttpWebRequest.GetResponse()
at DotNetOpenAuth.Messaging.StandardWebRequestHandler.GetResponse(HttpWebRequest request, DirectWebRequestOptions opt
ions)
--- End of inner exception stack trace ---
at DotNetOpenAuth.Messaging.StandardWebRequestHandler.GetResponse(HttpWebRequest request, DirectWebRequestOptions opt
ions)
at DotNetOpenAuth.Messaging.StandardWebRequestHandler.GetResponse(HttpWebRequest request)
at DotNetOpenAuth.Messaging.Channel.GetDirectResponse(HttpWebRequest webRequest)
at DotNetOpenAuth.Messaging.Channel.RequestCore(IDirectedProtocolMessage request)
at DotNetOpenAuth.Messaging.Channel.Request(IDirectedProtocolMessage requestMessage)
at DotNetOpenAuth.OAuth2.ClientBase.UpdateAuthorizationWithResponse(IAuthorizationState authorizationState, EndUserAu
thorizationSuccessAuthCodeResponse authorizationSuccess)
at DotNetOpenAuth.OAuth2.UserAgentClient.ProcessUserAuthorization(IAuthorizationState authorizationState, IDirectedPr
otocolMessage response)
at DotNetOpenAuth.OAuth2.UserAgentClient.ProcessUserAuthorization(Uri actualRedirectUrl, IAuthorizationState authoriz
ationState)
at Google.Apis.Authentication.OAuth2.DotNetOpenAuth.NativeApplicationClient.ProcessUserAuthorization(String authCode,
IAuthorizationState authorizationState) in c:\code.google.com\google-api-dotnet-client\default_oauth2\Src\GoogleApis.Au
thentication.OAuth2\DotNetOpenAuth\NativeApplicationClient.cs:line 102
at Google.Apis.Samples.TasksOAuth2.Program.GetAuthorization(NativeApplicationClient arg) in c:\code.google.com\google
-api-dotnet-client\samples_oauth2\Tasks.SimpleOAuth2\Program.cs:line 73
at Google.Apis.Authentication.OAuth2.OAuth2Authenticator`1.LoadAccessToken() in c:\code.google.com\google-api-dotnet-
client\default_oauth2\Src\GoogleApis.Authentication.OAuth2\OAuth2Authenticator.cs:line 124
I noticed also that there is a different in the second request (in exchanging the code with a token): Authorization header was added to the request, and the body was missing my client_id and client_secret.
Similar code worked on old version - 4.0.0.11165,
Am I missing something?
I wonder if the problem is that the newer DNOA version supports putting client credentials in the HTTP header by default. If you create your Client class, passing in a different client credential provider into the constructor, it may work for you.
To change the behavior from using the HTTP Authorization header back to passing client credentials in the POST entity, instantiate your ClientBase-derived class passing in this as a parameter to the constructor:
ClientCredentialApplicator.PostParameter(clientSecret)

Youtube Data API-debugging authentication errors

Getting authentication errors when I try and obtain my upload authorization token
https://developers.google.com/youtube/2.0/developers_guide_protocol_error_responses
Using a packet sniffer, my first error message is>
401 Token invalid - Invalid token: Cannot parse AuthSub token:
In addition to perhaps improperly formatted Auth key value, I'm wondering exactly what headers I should be including for my upload auth request.
I am using the following though think clientId has been deprecated
"Authorization", "GoogleLogin auth=\"" + authToken + "\""
"X-GData-Client", clientId
"X-GData-Key", "key=" + devKey
After changing
"Authorization", "AuthSub token="+authToken
to
Authorization", "GoogleLogin auth="+authToken
in my request I no longer get 'Cannot parse AuthSub token' error message but
I still get
Error #2032: Stream Error. URL: http://gdata.youtube.com/action/GetUploadToken
<errors>
<error>
<domain>yt:authentication</domain>
<code>Unknown</code>
</error>
</errors>
Stumped. Would really appreciate any feedback as I'm not even certain now where my error(s) exist!
ok working but not really sure how:)
Am using these 2 headers in my POST request to
'http://gdata.youtube.com/action/GetUploadToken'
"Authorization", "GoogleLogin auth="+authToken
"X-GData-Key", "key=" + devKey
And also needed to associate my youtube user developer credentials with a channel
https://groups.google.com/forum/#!msg/youtube-api-gdata/76x8vaADJWM/36O05FD7mC0J
A packet sniffer or at least adding support to read the XMl error responses is essential!
I resolved this problem by providing the correct developer key

Resources