oAuth2 with iOS (part 1)

So, now we are finished with the Android app, we start with creating the iOS app. For previous posts in this series, see here :).
Currently, Apple announced iOS 8 with their new swift language and a new xCode on WWDC a few days ago. While I am not going to use Swift for this tutorial, I will use Xcode 6 and iOS 8. I haven’t tried any of this code on previous iOS/xcode versions, however I guess it should not be a issue.

Lets start with the basics. We will use the same approach as with the Android app. We have a local Database (In this case we use core data, more on that later on), a simple table view, and a few buttons to add or delete data.
To implement the Core Data stuff, I use some code from tutorials created by Paul Hegarty. I won’t be going very deep into the Core Data related stuff, it is explained very well by Paul.

All code and examples I provide here are tested/shown on a iPad mini retina or a iPhone 5S. Both have iOS 8 Beta 1.

Like with he Android app, all code can be found in GIT. The code like at the end of the project can be found here.

Continue reading

oAuth2 with Android (Part 3)

This is the fifth post in the series over oAuth with Symfony2, iOS and Android. All posts in this serie can be found here. With this post we are finishing up the android app as much as possible with the ability to add new items (Via the API) and adding items we got from the API into the list. All code from this post can be found here at Github. Because wordpress doesn’t seem to work that well with the included code in this blog, I suggest to use the code from the repository. I created a tag in the repo for the part that was at the end of this third part. Lets start with adding a new Demo. This can be done by sending a POST request to api.host/demos. Before we start with creating the demos, we need to fix a issue in our requests. Volley standard adds a Content-Type with post. We don’t want this Content-Type because we send JSON, not a form. We also added by default a Content-Type header, but that causes issues if data isn’t JSON, or if we send POST, because Volley automatically adds the Content-Type as well. AbstractRequest.java:

package sohier.me.saiod.android;

import android.util.Log;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.google.api.client.auth.oauth2.Credential;
import com.google.gson.Gson;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

public abstract class AbstractRequest extends Request {

    private final Map headers;
    private final Response.Listener listener;

    protected Object bodyObject = null;

    /**
     * Create the URL for calling the API.
     * @param HOST Hostname
     * @param cred Credentials for the oAuth api.
     * @param url The path for the API.
     * @return full URL.
     */
    private static String makeUrl(String HOST, Credential cred, String url) {
        url = HOST + url;
        url += "?access_token=";
        url += cred.getAccessToken();

        Log.d("AbstractRequest", "URL: " + url);

        return url;
    }

    /**
     *
     * @param method HTTP method
     * @param path Path of the call
     * @param host hostname
     * @param creds Credentials
     * @param headers Headers for the HTTP request
     * @param listener Response listener
     * @param errorListener error Listener
     */
    public AbstractRequest(int method, String path, String host, Credential creds, Map headers, Response.Listener listener, Response.ErrorListener errorListener) {
        super(method, makeUrl(host, creds, path), errorListener);

        if (headers == null) {
            headers = new HashMap();
        }
        headers.put("Accept", "application/json");

        this.headers = headers;
        this.listener = listener;
    }

    @Override
    public Map getHeaders() throws AuthFailureError {
        return headers != null ? headers : super.getHeaders();
    }

    @Override
    protected void deliverResponse(T response) {
        listener.onResponse(response);
    }

    @Override
    public byte[] getBody() throws AuthFailureError
    {
        if (bodyObject == null)
        {
            return super.getBody();
        }
        Gson gson = new Gson();

        try {
            Log.d("saiod", new String(gson.toJson(bodyObject).getBytes("utf-8")));

            byte[] bt = gson.toJson(bodyObject).getBytes("utf-8");
            return bt;
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);

        }
    }
}

Continue reading

oAuth2 with Android (Part 2)

This is the fourth post in the series over oAuth with Symfony2, iOS and Android. All posts in this serie can be found here.
With this post we are continuing to work on the Android app, and start adding the needed oAuth libraries and adding network functionality.

All code from this post can be found here at Github. Because wordpress doesn’t seem to work that well with the included code in this blog, I suggest to use the code from the repository. I created a tag in the repo for the part that was at the end of this second part.

I assume within this post that you are at the same stage with your code as we ended in part 1.

To handle all oAuth related requests we are going to use a library that does all the hard work for us. This library can be found on Github.

Lets start with adding this library to our app. Because I am using Android Studio, dependencies and things like that are not handled by maven, but with Gradle. You should edit the build.gradle which is located in the android directory, not in the root!
Add in the dependencies section the next line:

compile 'com.wu-man:android-oauth-client:0.0.3'

Continue reading