Member Avatar for Falcon25

Hello everyone, I am currently working on an Android application that I hope will one day link to the Quizlet website so it can download flashcards from their website: https://quizlet.com/api/2.0/docs/ and people can use them, otherwise this app is just going to be a menu.

This is the first time I have tried working with an API and to be honest, I am completely lost. I have read through documentation online and such but I really can't grasp where to begin even. So any help in this area would be massively appreciated.

First of all, I am using the Eclipse Juno environment. As far as I am aware Quizlet uses the OAuth 2.0 protocol, they give you sample PHP code that gains authentication from the site- I don't even know how to test this. Would I just download the eclipse PHP plugin, then create a PHP file with the code inside and call it from a .java file? Or is this completely wrong? I know I will have to alter some permissions within the android manifest but from here on I am lost and the only examples I can find are for iOS and they do not help.

Again any help in this area would be great, thanks for reading.

From what is available without creating new account you need to first obtain token line 45 and only after that you can do login with OAuth. Replies from server are in JSON format so you should either know JSON API provided with Android or better to replace it with GSON or Jackson. Their documentation provides some Sample Calls that you can use for start. Beside trying to code all calls you can try to run it through CURL tool (you will have to download it for Windows, on Unix+Mac it is provided)

Member Avatar for Falcon25

Hey peter_budo thanks for the help, I am going to try and run some code through the CURL tool like you mentioned, just out of interest why is it better to replace the JSON API with GSON or Jackson?

I'm gone use JSON from here to demonstrate what I mean.
To read simple JSON as this

{
    "age":100,
    "name":"mkyong.com",
    "messages":["msg 1","msg 2","msg 3"]
}

You would need with default Android JSON API have to do something like this

    JSONObject jsonObject = (JSONObject) obj;

        String name = (String) jsonObject.get("name");
        System.out.println(name);

        long age = (Long) jsonObject.get("age");
        System.out.println(age);

        // loop array
        JSONArray msg = (JSONArray) jsonObject.get("messages");
        Iterator<String> iterator = msg.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }

Just see that cumpsy way of iterating through messages. This can be solved by using above libraries.
With Gson I would simple declare POJO (plain old java object)

public class Something{
    @SerializableName("age")
    private int age;
    @SerializableName("name")
    private String name;
    @SerializableName("messages")
    private List<String> messages = ArrayList<String>();

    public Something(int age, String name, List<String> messages){
        this.age = age;
        this.name = name;
        this.messages = messages;
    }

    //your getter methods here, no need for setters
}

and then use Gson to extract Something from JsonObject

JsonObject jsonObject = //Read server reply and get it as InputStream, pass it to Reader and use JsonParser to get you JsonObject
Gson gson = new Gson();
Something something = gson.fromJson(jsonObject, Something.class)

JAckson library works in similar way with annotations, I'm just more familiar with Gson.
This is much cleaner and simpler, since when JSON structure get more complicated you can replace primitive types with complex POJO as need it. Example company details including contact people can be represented as Company with name, Address, List<Contact>. Hope this helps

commented: Very helpful thank you +1
Member Avatar for Falcon25

Hi peter_budo, I apologise for my lack of experience in this, do you know how I can actually go about obtaining the token from line 45 of the PHP code inside my Android project? It is probably something really simple and I am missing something but I cannot for the life of me understand how to interact with this PHP code from within my project.

You will need to contact their support because according to documentation you should be able to authenticate with following command

curl -H "Authorization: Bearer YOUR_ACCESS_TOKEN" \
https://api.quizlet.com/2.0/users/YOUR_USERNAME

I tried it and it failed. For once I'm not aware that command can contain back slash "\", but even without it there is error message
However calls to cards are working, for example

curl https://api.quizlet.com/2.0/sets/415?client_id=YOUR_CLIENT_ID&whitespace=1

would return JSON object representing US state capitals

Member Avatar for Falcon25

Hello, i've spent far too many hours trying to retrieve the access token by following the user authentication flow https://quizlet.com/api/2.0/docs/authorization_code_flow/ everything is working fine until I get to step 2 which is constantly returning:

curl: (6) Could not resolve host: Authorization; Host not found

The request that I am using in cURL command line is this:

curl -i --data -H "Authorization: ENTER_BASIC_AUTHORISATION_CODE" -H "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" -u "grant_type=authorization_code&code=ENTER_GENERATED_CODE_FROM_SITE&redirect_url=ENTER_APP_URL" api.quizlet.com/oauth/token

I have emailed Quizlet but waiting for a response. The access token is proving to be rather annoying.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.