通过Google API检索电子邮件和电话号码

时间:2014-03-11 07:36:06

标签: coldfusion google-plus

我正在使用Coldfusion使用Google API,因此请拨打以下网址:

https://www.googleapis.com/plus/v1/people/{userID}?key={MyGoogleKey}

我能够获得用户的详细信息。无论他们在谷歌加帐户中分享了什么。缺少的一件事是他们的电子邮件地址和电话号码(如果有的话)。

我想,我需要再次拨打API来获取电子邮件和电话号码,但我正在努力解决这个问题。这是我试图使用的代码:

<cfset objGoogPlus = createObject("component","services.auth").init(apikey="#application.google_server_key#",parseResults=true)>
    <cfdump var="#objGoogPlus.people_get(userID='#data.id#')#">


<cffunction name="init" access="public" output="false" hint="I am the constructor method.">
        <cfargument name="apiKey"           required="true"     type="string"                   hint="I am the application's API key to access the services." />
        <cfargument name="parseResults"     required="false"    type="boolean"  default="false" hint="A boolean value to determine if the output data is parsed or returned as a string" />
            <cfset variables.instance.apikey        =   arguments.apiKey />
            <cfset variables.instance.parseResults  =   arguments.parseResults />
            <cfset variables.instance.endpoint      =   'https://www.googleapis.com/plus/v1/' />
        <cfreturn this />
    </cffunction>

<cffunction name="getparseResults" access="package" output="false" hint="I return the parseresults boolean value.">
        <cfreturn variables.instance.parseResults />
    </cffunction>

<cffunction name="people_get" access="public" output="false" hint="I get a person's profile.">
        <cfargument name="userID"           required="true"     type="string"                                   hint="The ID of the person to get the profile for. The special value 'me' can be used to indicate the authenticated user." />
        <cfargument name="parseResults"     required="false"    type="boolean"  default="#getparseResults()#"   hint="A boolean value to determine if the output data is parsed or returned as a string" />
            <cfset var strRequest   =   variables.instance.endpoint & 'people/' & arguments.userID & '?key=' & variables.instance.apikey />
        <cfreturn getRequest(URLResource=strRequest, parseResults=arguments.parseResults) />
    </cffunction>

<cffunction name="getRequest" access="private" output="false" hint="I make the GET request to the API.">
        <cfargument name="URLResource"  required="true" type="string"   hint="I am the URL to which the request is made." />
        <cfargument name="parseResults" required="true" type="boolean"  hint="A boolean value to determine if the output data is parsed or returned as a string" />
            <cfset var cfhttp   =   '' />
                <cfhttp url="#arguments.URLResource#" method="get" />
        <cfreturn handleReturnFormat(data=cfhttp.FileContent, parseResults=arguments.parseResults) />
    </cffunction>

    <cffunction name="handleReturnFormat" access="private" output="false" hint="I handle how the data is returned based upon the provided format">
        <cfargument name="data"         required="true" type="string"   hint="The data returned from the API." />
        <cfargument name="parseResults" required="true" type="boolean"  hint="A boolean value to determine if the output data is parsed or returned as a string" />
            <cfif arguments.parseResults>
                <cfreturn DeserializeJSON(arguments.data) />
            <cfelse>
                <cfreturn serializeJSON(DeserializeJSON(arguments.data)) />
            </cfif>
    </cffunction>

    <cffunction access="public" name="getProfileEmail" returntype="any" returnformat="json">
      <cfargument name="accesstoken" default="" required="yes" type="any"> 
      <cfhttp url="googleapis.com/oauth2/v1/userinfo"; method="get" resolveurl="yes" result="httpResult"> 
        <cfhttpparam type="header" name="Authorization" value="OAuth #arguments.accesstoken#"> 
        <cfhttpparam type="header" name="GData-Version" value="3"> 
      </cfhttp> 
      <cfreturn DeserializeJSON(httpResult.filecontent.toString())> 
    </cffunction>

我不想说什么,但我使用了以下方法,它似乎带来了电子邮件地址,而不是采用新方式,但采用旧方式:

<cfset googleLogin = objLogin.initiate_login(
                loginUrlBase = "https://accounts.google.com/o/oauth2/auth",
                loginClientID = application.google_client_id,
                loginRedirectURI = application.google_redirecturl,
                loginScope = "https://www.googleapis.com/auth/userinfo.email"
            )>

2 个答案:

答案 0 :(得分:3)

我通过这篇博客文章(http://www.raymondcamden.com/index.cfm/2014/2/20/Google-SignIn-and-ColdFusion)讨论了Google+并登录,是的,我知道仅仅指向博客文章是不好的,所以我的辛勤工作都不会被SO赞赏(叹息) )。正如囚徒所说,你得到的是基于最初要求的范围。以下是有关其他范围的文档:https://developers.google.com/+/api/oauth#login-scopes。电子邮件在那里,但据我所知,不是电话。说实话,无论如何,我都没有在Google+上看到我的手机#。

答案 1 :(得分:0)

根据您添加的“getProfileEmail”功能,您似乎正在使用userinfo范围和访问点。 Google has announced that this method is deprecated将于2014年9月删除。

您应该切换到使用people.get method并将email范围添加到Raymond在其博客上列出的G +登录按钮。

<强>更新

在致电objLogin.initiate_login时,您应更改loginScope参数以使用 个人资料电子邮件范围至少(请参阅https://developers.google.com/+/api/oauth#scopes以获取您可能希望使用的更完整的范围列表)。所以它可能看起来像:

<cfset googleLogin = objLogin.initiate_login(
         loginUrlBase = "https://accounts.google.com/o/oauth2/auth",
         loginClientID = application.google_client_id,
         loginRedirectURI = application.google_redirecturl,
         loginScope = "profile email"
       )>

然后,您可以使用people.get API call获取所需信息。但是,查看您的getRequest功能,似乎使用API​​密钥调用 people.get ,这不足以获取电子邮件 - 它只能获取公共信息。您需要以与getProfileEmail函数中调用 userinfo 相同的方式调用 people.get 。我不太了解ColdFusion,但您应该能够调整此函数来调用people.get endpoint