OAuth的使用谷歌API创建混合应用程序 [英] oAuth using Google API creating Hybrid App
问题描述
我想创建使用谷歌的OAuth登录页面。
为了这个,我已经创建了一个clientId。
在客户端ID我做了针对Android创建,因为我相信我的应用程序将在Android设备上运行。
创建客户端ID - >安装的应用程序 - > Android版
我已经做了keytooling和SHA1签署并创建一个clientId
然后我创建了一个公开API访问即可。在这我已经创建了一个 Android的关键。我所提供的COM名称和SHA1细节。由于我使用的 INTEL XDK 很容易从那里得到的COM namespane。
在我的javascript code我做了这样的事情来构造URL。
VAR clientID的=CLIENT_ID =; VAR responeType =
&放大器; RESPONSE_TYPE = code; VAR范围=&放大器;范围=个人资料; VAR范围
=&放大器;范围= https://www.googleapis.com/auth/calendar.readonly一>; VAR的redirectUrl =&放大器; REDIRECT_URI =瓮:IETF:WG:OAuth的:2.0:OOB VAR URL
=\"https://accounts.google.com/o/oauth2/auth?\"+clientID+responeType+scope+redirectURL+\"&approval_prompt=auto\";
块引用>然后我用一个XHRRequest给调用这个网址
VAR XHR =新XMLHtt prequest(); xhr.open(GET,网址,FALSE);结果
xhr.onload =功能(){
如果(xhr.readyState == 4和&放大器; xhr.status == 200)
{结果
的console.log(this.response);
VAR RESP = this.response;}} xhr.onerror =功能(E){
警报(错误:+ E)} xhr.send();
块引用>我有一个连接到它的事件侦听器的按钮。
我是新来的Android和特别是到OAuth。我得到的所有排序这样的错误
为请这个code,开关复制到您的应用程序,并粘贴有:
当我按一下按钮我没有看到任何登录页从谷歌请求许可
但我可以看到的console.log数据来()。什么是错的,我感到在这里做。
如果有任何方式使用创建混合应用的HTML5请分享。
也请告诉我,如果我这样做写或wrong.Please指导我。-S
解决方案您可以使用此的oauth2
的jQuery
插件:的 https://github.com/krisrak/jquery-cordova-oauth2它与英特尔XDK
所有你需要做的是修改下面与您的OAuth用户端的参数和回调调用时将ACCESS_TOKEN开始拨打电话
$。的oauth2({
auth_url:https://accounts.google.com/o/oauth2/auth',
RESPONSE_TYPE:code',
TOKEN_URL:https://accounts.google.com/o/oauth2/token',
logout_url:https://accounts.google.com/logout',
CLIENT_ID:CLIENT-ID-FROM-GOOGLE',
client_secret:以客户秘密FROM-GOOGLE',
REDIRECT_URI:http://www.yourwebsite.com/oauth2callback',
other_params:{范围:'个人资料'}
},功能(令牌,响应){
makeAPICalls(标记);
},功能(错误响应){
警报(错误);
});I am trying to create a login page using google oAuth. For this i have created a clientID. In ClientID i have done created for Android since i believe i app would be running on an Android Device.
Create Client Id ->Installed Application -> Android
I have done keytooling and signed it with SHA1 and create a clientID
then i created a Public API Access. In this i have created an Android Key. I have provided the com name and SHA1 details. Since i am using an INTEL XDK it was easy to get the COM namespane from there.
In my javascript code i have done something like this to construct the URL.
var clientID ="client_id="; var responeType = "&response_type=code"; var scope ="&scope=profile"; var scope ="&scope=https://www.googleapis.com/auth/calendar.readonly"; var redirectURL ="&redirect_uri=urn:ietf:wg:oauth:2.0:oob"; var url ="https://accounts.google.com/o/oauth2/auth?"+clientID+responeType+scope+redirectURL+"&approval_prompt=auto";
then i have used an XHRRequest to give a call to this URL
var xhr = new XMLHttpRequest(); xhr.open("GET",url,false);
xhr.onload = function() { if(xhr.readyState==4 && xhr.status==200) {
console.log(this.response); var resp = this.response;} } xhr.onerror = function(e) { alert("Error : "+e) } xhr.send();
I have a button with event listener attached to it. I am new to android and especially to oAuth. I am getting all sort of errors such as
Please copy this code, switch to your application and paste it there:
when i click the button i do not see any login Page from google asking for permission but i can see data coming in console.log().
What is wrong that i am doing here.
If there are any way to create Hybrid APp using html5 please share. also please tell me if i am doing it write or wrong.Please guide me.
-S
解决方案You can use this
jQuery
plugin for Oauth2: https://github.com/krisrak/jquery-cordova-oauth2It works with Intel XDK
All you have to do is modify the call below with your Oauth Client parameters and the callback will have access_token to start making calls
$.oauth2({ auth_url: 'https://accounts.google.com/o/oauth2/auth', response_type: 'code', token_url: 'https://accounts.google.com/o/oauth2/token', logout_url: 'https://accounts.google.com/logout', client_id: 'CLIENT-ID-FROM-GOOGLE', client_secret: 'CLIENT-SECRET-FROM-GOOGLE', redirect_uri: 'http://www.yourwebsite.com/oauth2callback', other_params: {scope:'profile'} }, function(token, response){ makeAPICalls(token); }, function(error, response){ alert(error); });
这篇关于OAuth的使用谷歌API创建混合应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!