Github with coderwall Application in Rhomobile

1 min read

I recently tried to integrate github and coderwall in rhomobile application. Using github oauth we can authenticate user and show the following details of user and coderwall patches of user

1.User Repositories
2.User Forked Repositories
3.User Watched Repositories
4.User Following
5.User Follower

Now lets see how to authenticate user in github
First we need to register our application in github:developer site.we will use client_id and client_secret of registered application as shown

    [source language=”ruby”]
    def github_login
    local_callback_url =url_for( :action => :github_callback )
    call_back_url = RedirectServiceURL + “/” + ‘127.0.0.1:’ +
    System.get_property(‘rhodes_port’).to_s + local_call_back_url
    url =”https://github.com/login/oauth/authorize?client_id=xxxxx&
    redirect_uri=#{call_back_url}&scope=user,public_repo,repo,gist”
    WebView.navigate(url)
    end
    [/source]

From above method we can get @params[“code”] to access token of current user. We have used following callback url to redirect our rhombile application since rhomobile have random port number

    [source language=”ruby”]
    RedirectServiceURL = “http://redirectme.to” //used to redirect application url//
    [/source]

You can attain port number as

    [source language=”ruby”]
    System.get_property(‘rhodes_port’).to_s
    [/source]

Lets see how to process @params[code] to get user token as shown

    [source language=”ruby”]
    def github_callback
    code=@params[“code”]
    @@token=Rho::AsyncHttp.post(
    :url => “https://github.com/login/oauth/access_token?client_id=xxxxx&client_secret=xxxx&code=” + code
    )
    user_info = “https://github.com/api/v2/json/user/show?” + @@token[“body”]
    user=Rho::AsyncHttp.get(
    :url => user_info,
    )
    WebView.navigate( url_for :action => :listing )
    end
    [/source]

So far we saw how to authenticate user and get token. We can also get access to public, forked and watched repositories using Username as shown below

    [source language=”ruby”]
    my_repo_url = “https://github.com/api/v2/json/repos/show/”
    + Rho::RhoConfig.username #=> Get Public and forked repositories info
    my_watched_url = “https://github.com/api/v2/json/repos/watched/”
    + Rho::RhoConfig.username #=> Get watched repositories
    [/source]

Using callback events for the each url, we can manipulate data and display it
We can also list of followers and following users using the below url

    [source language=”ruby”]
    my_following_url = “https://github.com/api/v2/json/user/show/”
    + Rho::RhoConfig.username + “/following” #=> Following url
    my_followers_url = “https://github.com/api/v2/json/user/show/”
    + Rho::RhoConfig.username + “/followers” # => Followers list
    [/source]

Finally we will integrate Coderwall_page in github rhomobile application. Since Coderwall use github authentication to give patches, we can easily integrate coderwall page in github rhomobile application. We get details of user in coder wall in JSON format as shown

    [source language=”ruby”]
    user_details_url = “http://coderwall.com/#{Rho::RhoConfig.username}.json”
    result = Rho::AsyncHttp.get(:url => auth_url)
    [/source]

The execution of GET request will give User details.

Some of the screenshots taken from Android Mobile

      
I hope this blog will be helpful for integrating github authentication in rhomobile. Source code of application is available at https://github.com/spritle/rhodes_github_client_demo.Please feel free to improve and send me a pull request. Any suggestion or comments are welcome.

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *