victorholo
victorholo

Reputation: 115

Moodle - Trying to get token, receiving Forbidden 403

I am using the TokenHttpRequest class from the open-source project:

public String doHTTPRequest(String url){ 
        String responseBody = ""; 
        String token = "";

        DefaultHttpClient httpClient = new DefaultHttpClient();

        // Creating HTTP Post 
        HttpGet httpPost = new HttpGet(url); 

        try { 
            ResponseHandler<String> responseHandler=new BasicResponseHandler(); 
            HttpContext context = new BasicHttpContext();             context.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0");
            responseBody = httpClient.execute(httpPost, responseHandler, context); 

            JSONObject jObject = new JSONObject(responseBody);
            token = jObject.getString("token");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();            
        } catch (ClientProtocolException e) { 
            // writing exception to log 
            e.printStackTrace(); 
        } catch (IOException e) { 
            // writing exception to log 
            e.printStackTrace();       
        } 

        return token; 
    } 

It worked well until the moodle site im accessing updated to 2.7. Now the httpClient.execute line gives error Forbidden 403. In the browser the url works fine.

This is what i get:

09-24 17:42:58.246: W/System.err(8820): org.apache.http.client.HttpResponseException: Forbidden
09-24 17:42:58.246: W/System.err(8820):     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:71)
09-24 17:42:58.246: W/System.err(8820):     at org.apache.http.impl.client.BasicResponseHandler.handleResponse(BasicResponseHandler.java:59)
09-24 17:42:58.246: W/System.err(8820):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
09-24 17:42:58.246: W/System.err(8820):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
09-24 17:42:58.246: W/System.err(8820):     at moodle.android.moodle.helpers.TokenHttpRequest.doHTTPRequest(TokenHttpRequest.java:86)
09-24 17:42:58.246: W/System.err(8820):     at ro.example.app.Login$2.run(Login.java:170)
09-24 17:42:58.246: W/System.err(8820):     at java.lang.Thread.run(Thread.java:841)

and also:

09-24 17:13:51.986: I/entity(2975):  <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
09-24 17:13:51.986: I/entity(2975): <html><head>
09-24 17:13:51.986: I/entity(2975): <title>403 Forbidden</title>
09-24 17:13:51.986: I/entity(2975): </head><body>
09-24 17:13:51.986: I/entity(2975): <h1>Forbidden</h1>
09-24 17:13:51.986: I/entity(2975): <p>You don't have permission to access /login/token.php
09-24 17:13:51.986: I/entity(2975): on this server.</p>
09-24 17:13:51.986: I/entity(2975): </body></html>

When i try to get the entity using EntityUtils

Anyone had this problem?

Upvotes: 1

Views: 3308

Answers (2)

Chelsea Saffold
Chelsea Saffold

Reputation: 81

On Moodle 3.8, you can take the following steps to set it up:

  1. Enable web services at Site Administration -> Advanced Features.
  2. Enable Moodle mobile web service at Site Administration -> Plugins -> Web Services -> External Services.
  3. Enable your desired protocol at Site Administration -> Plugins -> Web Services -> Manage Protocols.
  4. Generate a token for the user you plan to use to make API calls at Site Administration -> Plugins -> Web Services -> Manage Tokens.

Upvotes: 2

Thpramos
Thpramos

Reputation: 441

Setting this up the first time is a real pain, but there are some steps that might help you figure out that.

  1. First of all, there is a known bug on Moodle that when you create an external service on the web, it does not create the shortname correctly on the database. You can fix it by accessing the database, going to the table external_services and adding manually the shortname that you are using on the Request.

  2. Enabling permission for a group. This is probably your problem, and to fix that you will need to go in Users>Permissions>Define roles. Once there edit one of the roles, enabling it to create token and access REST protocol. ( I usually allow create token for mobile and create token )

  3. After doing the last step be sure to assign this role to the user you are trying to access going to Assign roles in System and clicking to the role.

  4. Lastly if all that does not work, try searching the whole settings for any possible checkbox that says anything about WebService! :)

Hope that helps you and others like me that got a whole week stuck on that!

Upvotes: 2

Related Questions