how to set myself URL when make call by Twilio? - java

As you know, use Twilio to make a call, needs pass a URL tell Twilio to fetch instructions.
And I deploy my URL on http://1xx.16.102.129:8080/voice/voice.xml
Here is my code:
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Url","http://1xx.16.102.129:8080/voice/voice.xml"));
params.add(new BasicNameValuePair("To", "+1xxxxxxxxxxxx"));
params.add(new BasicNameValuePair("From", "+15086895110"));
CallFactory callFactory = client.getAccount().getCallFactory();
Call call = callFactory.create(params);
After the phone call end, I got exception like "502 Bad Gatewayā€¯". Can anyone tell me what is the problem?

Twilio evangelist here.
502 Bad Gateway is usually an indication that Twilio tried to request content from the URL you specified when initiating the outbound call but had trouble.
There are a lot of things that can cause this including things like problems resolving DNS, not including a Content-Type header in the response or network disruptions between Twilio and your server.
We have put together a number of troubleshooting tips that you can find on our website that you can use to help diagnose the problem.
Hope that helps.

Related

Migrate OOB in HTTP POST request to googleapis

We are making use of this end point - https://www.googleapis.com/oauth2/v4/token
to get the access token.
We make use of apace HTTP classes to make a POST request to this end point in this way -
HttpPost httpPost = new HttpPost(GET_ACCESS_TOKEN_API);
StringBuilder blr = new StringBuilder().append(CLIENT_ID).append("=")
.append((String) accountCredentials.get(CLIENT_ID)).append("&")
.append(CLIENT_SECRET).append("=")
.append((String) accountCredentials.get(CLIENT_SECRET))
.append("&").append(REFRESH_TOKEN).append("=")
.append((String) accountCredentials.get(REFRESH_TOKEN))
.append("&grant_type=refresh_token")
.append("&redirect_uri=urn:ietf:wg:oauth:2.0:oob");
// The message we are going to post
StringEntity requestBody = new StringEntity(blr.toString());
// the default content-type sent to the server is
// application/x-www-form-urlencoded.
requestBody.setContentType("application/x-www-form-urlencoded");
httpPost.setEntity(requestBody);
// Make the request
HttpResponse response = HttpUtils.getHttpClient().execute(httpPost);
There has been a recent intimation from google to migrate from out-of-band as they have plans to deprecate this.
We make use of it this way as you can see in the code above -append("&redirect_uri=urn:ietf:wg:oauth:2.0:oob");
GET_ACCESS_TOKEN_API is https://www.googleapis.com/oauth2/v4/token.
I saw some posts mentioning that we have to replace this redirect_uri to localhost.
Can someone explain exactly how this works and what change needs to be done to migrate this successfully ? I tried searching through the documentation to see if there any sample examples but couldn't find anything that matches our use case.
I am referring to this site -
https://developers.google.com/api-client-library/java/google-oauth-java-client/support
I tried to browse through samples, guides, but it mostly talks about different API's. I didn't find the github links that much useful.
Any help would be much appreciated.

Android login and redirect

I am trying to get some data from a webpage that needs login in a page. I could do with htmlunit but gave me problems import these libraries on Android. So I am trying to do it with apache http client.
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://inside.cineca.it/cgi-bin/uinside/marcature.pl");
BasicNameValuePair usernameBasicNameValuePair = new BasicNameValuePair("j_username", "user");
BasicNameValuePair passwordBasicNameValuePAir = new BasicNameValuePair("j_password", "pass");
List<NameValuePair> nameValuePairList = new ArrayList<NameValuePair>();
nameValuePairList.add(usernameBasicNameValuePair);
nameValuePairList.add(passwordBasicNameValuePAir);
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(nameValuePairList);
HttpResponse httpResponse = httpClient.execute(httpPost);
System.out.println(EntityUtils.toString(httpResponse.getEntity()));
But is giving me error. I am affraid i should manage the redirect but i don't know how. I would be happy if someone could give me some advice.
Best regards
You're performing an http POST to a login page. Maybe the server that serves you back the login page doesn't know to handle the post. Anyway, I am sure your intention is to perform the login actually. You need to figure out what is the server API where you should post the credentials and process the result. Performing a quick investigation with Chrome Network profiler, the login API for above link actually resides at: https://idp-is.cineca.it/idp/Authn/Multilogin. That is the URL that you should be using and there is where you need to POST.
Also, make sure the request you're making is done on a non-UI thread as this is a common mistake done by beginners.

Making comments on a wordpress.org blog on android

I am trying add a feature to add comments in my app for my blog. I was trying to achieve this through httpclient, but i am continuously failing to achieve this. Here is my code:
HttpClient client = new DefaultHttpClient();
HttpPost http = new HttpPost("http://universityoftrollogy.wordpress.com/wp-comments-post.php");
http.setHeader("Content-type","application/x-www-form-urlencoded;charset=UTF-8");
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("_wp_http_referer", referer));
nameValuePairs.add(new BasicNameValuePair("hc_post_as", "guest"));
nameValuePairs.add(new BasicNameValuePair("comment_post_ID", postId));
nameValuePairs.add(new BasicNameValuePair("comment_parent","0"));
nameValuePairs.add(new BasicNameValuePair("comment", cData));
nameValuePairs.add(new BasicNameValuePair("email", cEmail));
nameValuePairs.add(new BasicNameValuePair("author", cName));
http.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(http);
My client executes properly but I cannot see any comments on my post!
I am not sure why isn't it working but a possible reason may be that it is not handling redirects.
Can anyone help me by guiding me to the right way to achieve it? Any help would be greatly appreciated.
Edit : After checking my response, I found that the status line for response is 500.
When you work against a wordpress.com blog, you will move from one bug to the next,
without being able to diagnose the errors.
So my advise: Set up your own webserver and wordpress-blog on your development machine. It is not difficult.
Then you can check the error-log of your webserver what wordpress did not like.
And yes, you probably need a "nonce". You can read about in in the codex-page. But remember that the codex-page is written from the perspective of a wordpress-plugin-developer.
there is a parameter called xxx_none to validate user in wordpress. It is generated randomly, I think, when a client open a session.
you may need to try to open the page once, then parse that parameter (I think in your page, it is "highlander_comment_nonce"), then put it into the http request too?
Tip: if you are using chrome, there is tool in the developer tool which help you to copy the request in the cUrl format, so you can see all the parameters sent to server.

Facebook Scores API for Android

I'm working on integrating the Leaderboards API of FB with my app. And I'm kinda confused with all these permissions, user and app access tokens, and all that. I would be VERY grateful, if someone could explain to me, how to do it step by step, in a simple language. Assume that I'm really retarded and <14.
I really searched a lot to find a solution, but none of them work for me, and I'm sick of this after all day. I write in JAVA.
Okay, I did manage to get the POST and GET requests in the Graph API Explorer, it all works fine, though now I'm not sure how to get my http request in the app operational:
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://graph.facebook.com/me/scores");
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("score", "3000"));
try{
post.setEntity(new UrlEncodedFormEntity(pairs));
}
catch(UnsupportedEncodingException e)
{
}
try{
HttpResponse response = client.execute(post);
}
catch (IOException e1)
{
}
Another update: it had to be done with AsyncTask. And so I did, everything is almost fine, but this action requires an access token. I've searched A LOT, and I've found tones of information about access tokens, but none of which tells me, how to do it programmatically. What exactly do I have to do, to pass the access token to a HTTP request? (I've set the permissions:
authButton.setReadPermissions(Arrays.asList("user_likes", "user_status", "user_location", "user_birthday",
"user_activities", "user_games_activity"));
}
Facebook has an entire section on working with Android. https://developers.facebook.com/android/
Consider using the official Android SDK so that you don't have to handle authentication an re-write the wheel. https://developers.facebook.com/docs/getting-started/facebook-sdk-for-android/3.0/
Once done that see how this Android sample works https://developers.facebook.com/docs/tutorials/androidsdk/3.0/games/
If you want to do it without the SDK, it is still in your best interests to understand how Facebook does authentication using their SDK so you can build your own https://developers.facebook.com/docs/tutorials/androidsdk/3.0/games/authenticate/
Access tokens are the keys allowed by a user once they have granted access to your application
See more info at https://developers.facebook.com/docs/howtos/androidsdk/3.0/login-with-facebook/

Django-Piston and Python Client Vs Java Client

I've built a web service using Django-Piston that allows for POST and GET requests. As part of testing I wrote a quick Python script. Using the script I can successfully do both types of requests; however, when a client written in Java attempts to do a POST I get an error: "POST /api/service/ HTTP/1.1" 400 225 "-" "Apache-HttpClient/4.1 (java 1.5)"
My understanding is that http request message that is generated by any language has to be the same. In other words, if I test my web service using a python client and it works then it should work for all other languages that have a http library.
Here is the python code for the POST:
import urllib, urllib2
data = urllib.urlencode({'url': 'www.uvic.ca', 'name': 'uvic'})
url = 'http://xxx/api/service/'
req = urllib2.Request(url, data)
print urllib2.urlopen(req).read()
and here is the Java code:
HttpPost httpost = new HttpPost("http://xxx/api/service/");
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("name", "some_name"));
nvps.add(new BasicNameValuePair("url", "www.somename.com"));
httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
response = httpclient.execute(httpost);
entity = response.getEntity();
System.out.println(entity.getContentType());
System.out.println(EntityUtils.getContentCharSet(entity));
System.out.println(EntityUtils.toString(entity));
I'm starting to think that this is an apache configuration problem. I put in some debugging statements at the start of my method for POST and I'm not hitting them at all. This means that something is wrong with the urls.py file (which I doubt because it works in python) or something is weird with apache.
Please help. Thanks in advance.
A little search would help you a lot. This was the first Google result.
http://weblog.mattdorn.com/content/restful-web-apps-with-django-piston-and-ext-js/
The reason for the 400 errors is that
ExtJS is appending a charset onto the
Content-Type field, which causes
piston to not interpret the
Content-Type correcly. There is an
open issue for it at
http://bitbucket.org/jespern/django-piston/issue/121/content-type-is-not-being-split-against.
I was able to get the example working
after I applied the patch and did an
easy_install.
This is the second Google response.
https://bitbucket.org/jespern/django-piston/issue/99/bad-request-and-content-type-with-fix

Categories