Tutorial: Connecting to RESTful API services

network-upload-feat
Share on Facebook0Share on Google+1Tweet about this on TwitterShare on LinkedIn0

One of the powers of Corona SDK is the ability to connect to various online services to get data into your app. There are several ways of getting data in. You could use an RSS feed which is a collection of well-defined XML. Another method is SOAP which allows you to get data into apps connected to a server — however, SOAP uses a fairly complex authentication method which is challenging to deal with in Corona. Another method is REST (or RESTful) APIs which are based on standard HTTP protocols and are generally easier to authenticate.

Overview

Many services offer their data via REST and you can use the standard HTTP “verbs” to access that data:

  • GET
  • POST
  • PUT
  • DELETE

GET is used to “get” (fetch) data. POST and PUT can be both used to send data to the server, and DELETE is used to remove data (if permitted).

REST uses standard HTTP URLs to define the API. For instance, with Twitter, you could access…

https://api.twitter.com/1.1/friends/list.json

…with a GET request to get a list of a user’s friends.

Most services run their APIs over a secure protocol (https://). Following the host name is commonly the API version — in this case /1.1 indicates that this is version 1.1 of the API. This allows services to change their API definitions to add/update features or change data without breaking older apps that utilize the previous version. Following this is typically the category of functionality. In this example, all of Twitter’s “friends” APIs are grouped under the /friends family. Finally, the specific /list API will fetch a list of the current user’s friends, conveniently formatted in JSON which is easily compatible with Corona (alternatively, some services allow you to request XML data by specifying .xml instead of .json).

Authentication

Most REST APIs require some form of verification or authentication, in particular if you’re trying to add or update data. Authentication can occur in various ways, for example by passing the username and password as part of the domain name:

http://username:password@api.yoursite.com/someapi/dosomething.json

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

This username:password method is a shortcut to using a headers table. Sometimes the username and password will be referred to as the “API Key” and “secret” and they may need to be encoded in some fashion such as MD5.

An alternate method is to use a headers table, discussed in more detail below. Yet another method is oAUTH, and while explaining oAUTH is beyond the scope of this tutorial, Corona SDK includes an example oAUTH 1.0 module in the following local folder: /SampleCode/Networking/Twitter.

Making API Calls

In Corona SDK, your REST API calls are made using network.request(). For this API, you provide a URL, an HTTP “verb,” a callback function to handle the completed request, and an optional table of information in which to handle headers and data passed to the server.

Let’s look at a simple example using basic authentication (no headers):

Alternatively, if your service can’t use basic authentication as part of the URL, you need to set up headers:

 

Conclusion

Every service will have its own methods to call, so you need to research the specific API calls and learn what HTTP verbs are expected, as well as which parameters. Also, each service seems to implement authentication a little differently, but this tutorial should get you started on using REST-based APIs from various providers.

 

[출처] https://coronalabs.com/blog/2015/06/02/tutorial-connecting-to-restful-api-services/

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
31 유니티에서 턴제 게임 구현하기 졸리운_곰 2017.05.09 4621
30 Unity3D 씬 뷰 file 졸리운_곰 2017.04.23 678
29 강좌 : 폰갭 앱 개발 PhoneGAP App Development file 졸리운_곰 2017.04.17 648
28 Visual Novel Resources (character art, backgrounds, music, etc.) 졸리운_곰 2017.03.16 423
27 게임 리소스 모음 - Game Resource Collection 졸리운_곰 2017.03.16 391
26 Optimize your Ionic Testing with Wallaby.js, Bard.js, and WebStorm file 졸리운_곰 2017.02.06 1034
25 Ionic Framework를 이용하여 하이브리드앱 개발하기 file 졸리운_곰 2016.12.14 775
24 Ionic 프레임워크 각종 탬플릿 예제 : A Comprehensive List Of Ionic Starter Apps file 졸리운_곰 2016.11.27 645
23 깃허브에 있는 오픈소스 게임들 : Games on GitHub file 졸리운_곰 2016.11.27 1613
» [corona sdk] Tutorial: Connecting to RESTful API services 졸리운_곰 2016.06.05 460
21 Top 5 Tools for Game Developers file 졸리운_곰 2016.05.20 370
20 16 top mobile game development tools file 졸리운_곰 2016.05.20 733
19 lua for mobile game development tool2 / 프로그래밍 언어 루아로 가능한 모바일 게임 제작 졸리운_곰 2016.05.15 435
18 Corona SDK: How to communicate between lua and javascript 졸리운_곰 2016.04.20 472
17 lua for java luaj : 자바에서 루아 언어 사용 LuaJ 졸리운_곰 2016.04.20 909
16 백엔드리스 서버 파일 전송 : BackendlessException{ code: 'Internal client exception', message: 'null' } 졸리운_곰 2016.04.11 694
15 백엔드레스 서버 / 안드로이드 예제 / backendless server / android example file 졸리운_곰 2016.04.05 745
14 parse parse 한글 메뉴얼 졸리운_곰 2016.03.25 378
13 LUA 루아 언어용 언어 변환기 lua language translator file 졸리운_곰 2016.03.07 500
12 유니티3D '퐁' 게임 개발 file 졸리운_곰 2016.02.01 1111
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED