百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
O

oauth-phonegap

> 编程语言
开源

Apache Cordova/PhoneGap 的 OAuth.io 插件

196 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

Apache Cordova/PhoneGap 的 OAuth.io 插件

OAuth.io Cordova/Phonegap SDK

This is the Cordova/Phonegap SDK for OAuth.io. OAuth.io allows you to integrate 100+ providers really easily in your web app, without worrying about each provider's OAuth specific implementation.

Installation

  • This plugin is supported on PhoneGap (Cordova) v3.0.0 and above.

OAuth.io Requirements and Set-Up

To use this plugin you will need to make sure you've registered your OAuth.io app and have a public key (please check https://oauth.io/docs).

Getting the SDK

Run the following command in your project directory. If you are using Phonegap, use the phonegap command. If you are using Cordova, use the cordova command.

bash
$ cordova plugin add https://github.com/oauth-io/oauth-phonegap

Whitelisting API URLs

OAuth.io will try to access various URLs, and their domains must be whitelisted in your config.xml in the `` tag's origin attribute :

xml

    
    
    

You can either add each domain separately, depending on the providers you use:

  • graph.facebook.com
  • api.twitter.com
  • github.com
  • ...

Or you can allow all domains with * like this :

xml

Integrating in your project

The OAuth object is automatically added to window when you include the plugin, so you don't need to add it yourself.

In your JavaScript, add this line to initialize OAuth.js. You can get the public key of your app from your OAuth.io dashboard.

javascript
OAuth.initialize('your_app_public_key');

Usage

To connect your user using facebook (as an example):

javascript
//Using popup (option 1)
OAuth.popup('facebook')
.done(function(result) {
 //use result.access_token in your API request 
 //or use result.get|post|put|del|patch|me methods (see below)
})
.fail(function (err) {
 //handle error with err
});

Using the cache

As of version 0.2.0, you can use the cache feature. This prevents the user from having to log in to the provider through a popup everytime he wants to access the app.

To use the cache, pass an options object to your popup method like this :

javascript
OAuth.popup('facebook', {
    cache: true
})
.done(function(result) {
  //use result.access_token in your API request 
  //or use result.get|post|put|del|patch|me methods (see below)
})
.fail(function (err) {
  //handle error with err
});

That way, your user will have to login to the provider only if the request token has not been retrieved yet or has expired.

Making requests

You can make requests to the provider's API manually with the access token you got from the popup or callback methods, or use the request methods stored in the result object.

GET Request

To make a GET request, you have to call the result.get method like this :

javascript
//Let's say the /me endpoint on the provider API returns a JSON object
//with the field "name" containing the name "John Doe"
OAuth.popup(provider)
.done(function(result) {
    result.get('/me')
    .done(function (response) {
        //this will display "John Doe" in the console
        console.log(response.name);
    })
    .fail(function (err) {
        //handle error with err
    });
})
.fail(function (err) {
    //handle error with err
});

POST Request

To make a POST request, you have to call the result.post method like this :

…

PUT Request

To make a PUT request, you have to call the result.post method like this :

…

PATCH Request

To make a PATCH request, you have to call the result.patch method like this :

…

DELETE Request

To make a DELETE request, you have to call the result.del method like this :

javascript
//Let's say the /picture?id=picture_id endpoint on the provider waits for
//a DELETE request to delete a picture with the id "84"
//and returns true or false depending on the user's rights on the picture
OAuth.popup(provider)
.done(function(result) {
    result.del('/picture?id=84')
    .done(function (response) {
        //this will display true if the user was authorized to delete
        //the picture
        console.log(response);
    })
    .fail(function (err) {
        //handle error with err
    });
})
.fail(function (err) {
    //handle error with err
});

Me() Request

The me() request is an OAuth.io feature that allows you, when the provider is supported, to retrieve a unified object describing the authenticated user. That can be very useful when you need to login a user via several providers, but don't want to handle a different response each time.

To use the me() feature, do like the following (the example works for Facebook, Github, Twitter and many other providers in this case) :

javascript
//provider can be 'facebook', 'twitter', 'github', or any supported
//provider that contain the fields 'firstname' and 'lastname' 
//or an equivalent (e.g. "FirstName" or "first-name")
var provider = 'facebook';

OAuth.popup(provider)
.done(function(result) {
    result.me()
    .done(function (response) {
        console.log('Firstname: ', response.firstname);
        console.log('Lastname: ', response.lastname);
    })
    .fail(function (err) {
        //handle error with err
    });
})
.fail(function (err) {
    //handle error with err
});

Filtering the results

You can filter the results of the me() method by passing an array of fields you need :

javascript
//...
result.me(['firstname', 'lastname', 'email'/*, ...*/])
//...

Contributing

Issues

Feel free to post issues if you have problems while using this SDK.

Pull requests

You are welcome to fork and make pull requests. We appreciate the time you spend working on this project and we will be happy to review your code and merge it if it brings nice improvements :)

If you want to do a pull request, please mind these simple rules :

  • One feature per pull request
  • Write lear commit messages
  • Unit test your feature : if it's a bug fix for example, write a test that proves the bug exists and that your fix resolves it.
  • Write a clear description of the pull request

If you do so, we'll be able to merge your pull request more quickly :)

Testing the SDK

Unit Testing

To test the SDK, you first need to install the npm modules jasmine-node and istanbul (to get the tests coverage) :

bash
$ sudo npm install -g [email protected] istanbul

Then you can run the testsuite from the SDK www directory :

bash
$ jasmine-node --verbose tests/unit/spec

Once you've installed istanbul, you can run the following command to get coverage information :

bash
$ npm test

The coverage report is generated in the coverage folder. You can have a nice HTML render of the report in coverage/lcof-report/index.html

Running the included samples

Create a new project as described in the PhoneGap documentation. For example:

bash
$ phonegap create oauthio-test com.example.oauthio-test OAuthioTest
$ cd oauthio-test
$ phonegap install android

Install OAuth.io plugin into the project

bash
$ phonegap local plugin add https://github.com/oauth-io/oauth-phonegap

Replace the generated example index.html with the one included in the example folder, and copy jquery.

A valid key is provided, but you can do your own app on OAuth.io. Also, please check that your config.xml file contains `` or accept oauth.io and the provider's domain (e.g. graph.facebook.com).

Plug your phone & run it ! (or add --emulate)

bash
$ phonegap run android

License

This SDK is published under the Apache2 License.

More information in oauth.io documentation

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言