I am planning to create an app that uses JavaScript and it needs to use OAuth to authenticate user for a website. Can anyone help me out please? Any sample code? I know about the Google Code Javascript OAuth library but I am not sure how to implement that..
OAuth is an authentication protocol that allows you to approve one application interacting with another on your behalf without giving away your password.
Server-side apps are the most common type of application encountered when dealing with OAuth servers. These apps run on a web server where the source code of the application is not available to the public, so they can maintain the confidentiality of their client secret.
This document explains how to implement OAuth 2.0 authorization to access Google APIs from a JavaScript web application. OAuth 2.0 allows users to share specific data with an application while keeping their usernames, passwords, and other information private.
Google APIs use the OAuth 2.0 protocol for authentication and authorization. Google supports common OAuth 2.0 scenarios such as those for web server, client-side, installed, and limited-input device applications. To begin, obtain OAuth 2.0 client credentials from the Google API Console.
There is a JS client implementation for OAuth here: https://developers.google.com/identity/protocols/OAuth2UserAgent
It contains example code to get you running. Basically, what you do is this:
var url = "..."; var accessor = { token: "...", tokenSecret: "...", consumerKey : "...", consumerSecret: "..." }; var message = { action: url, method: "GET", parameters: {...} }; OAuth.completeRequest(message, accessor); OAuth.SignatureMethod.sign(message, accessor); url = url + '?' + OAuth.formEncode(message.parameters); // send request to 'url' ...
Cheers, Matthias
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With