سؤال

أحاول بناء عميل Tumblr في Android ولكني أدير رمز الاستجابة 403 - محظور.

تقترح مستندات API أنني أستخدم معلمات البريد الإلكتروني وكلمة المرور للتحقق من صحة بيانات الاعتماد والحصول على معلومات الحساب. لقد جربت الرمز التالي ولكن كما هو محدد ، احصل على رمز استجابة 403. هل يمكن لأي شخص اقتراح ما أفعله خطأ.

    String url = "http://www.tumblr.com/api/authenticate";
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 20000);
    HttpConnectionParams.setSoTimeout(params, 20000);       
    DefaultHttpClient httpclient = new DefaultHttpClient(params);   

    UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(_username + ":" + _password); 
    httpclient.getCredentialsProvider().setCredentials(new AuthScope(null,-123),credentials);       

    HttpPost method = new HttpPost(url);        
    HttpResponse response = httpclient.execute(method);
    response.getStatusLine().toString(); // this gives me a 403 res code (Forbidden)..
هل كانت مفيدة؟

المحلول

إليك ما أستخدمه في تطبيقي للمصادقة:

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.tumblr.com/api/authenticate");

    try {
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("email", Username));
    nameValuePairs.add(new BasicNameValuePair("password", Password));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    HttpResponse response = httpclient.execute(httppost);
    if (response.getStatusLine().getStatusCode() != 200) {
        return false;
    }
    return true;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top