2013-01-09 26 views
0

我试图使用get和list方法来使用Google plus comment。在官方网站中,它表示(所有的API调用都需要一个OAuth 2.0令牌或一个API密钥。)并且我尝试了发送GET请求,而没有使用OAuth步骤,它可以工作,它返回json格式数据。我的问题是,在使用Google + API之前,OAuth必须要求吗?在使用Google + API之前必须使用OAuth

回答

1

这取决于您正在尝试获取的数据。

https://developers.google.com/+/api/oauth记录了使用OAuth的好处,但一般情况下,如果您想要获取私人个人资料数据,或者希望使用/ me/URL快捷方式,则需要使用OAuth,并且可能,另外使用一个App Key。如果您对所有感兴趣的都是公共数据,则可以使用应用程序密钥。

+0

我只想从谷歌+评论是否意味着我可以跳过OAuth的一部分。 Google Plus的标志看起来不相关? – user1961278

1

您是否可以做到这一点的简短答案是您可以在没有OAuth的情况下从Google+获得评论。

至于你怎么做到这一点,我不确定你在做什么,但下面的代码显示了这是如何在JavaScript中完成的。

这里使用的API调用可以在API浏览器直接尝试:

一个demo of this code is here

您需要使用来自Google APIs console的Google+ API的项目的API密钥(简单密钥)。设置项目时,您只需从服务部分启用Google+ API即可。

首先,使用公共数据API抢活动:

// Gets the activities for a profile 
function getActivities(profileID){ 
    var activities = null;  
    var URL  = "https://www.googleapis.com/plus/v1/people/" + profileID +   "/activities/public?alt=json&key=" + key; 
    var request = new XMLHttpRequest(); 
    request.open('GET', URL, false); 
    request.send(); // because of "false" above, will block until the request is done 
        // and status is available. Not recommended, however it works for simple cases. 

    if (request.status === 200) { 
    if (debug) console.log("retrieved activities \n\n"); 
    var activities = jQuery.parseJSON(request.responseText).items; 
    console.log("Discovered " + activities.length + " activities"); 
    }else{ 
    handleRequestIssue(request); 
    } 

    return activities; 
} 

下面的代码遍历活动

for (var i=0; i < activities.length; i++) { 
     console.log("trying to do something with an activity: " + i); 
     var activity = activities[i]; 

     console.log(activity.id); 
} 

接下来,你可以使用活动ID检索每个活动的意见:

function getCommentsForActivity(activityID){ 
    var comments = "";  
    var URL  = "https://www.googleapis.com/plus/v1/activities/" + activityID + "/comments?alt=json&key=" + key; 
    var request = new XMLHttpRequest(); 
    request.open('GET', URL, false); 
    request.send(); // because of "false" above, will block until the request is done 
       // and status is available. Not recommended, however it works for simple cases. 

    if (request.status === 200) { 
    if (debug) console.log(request.responseText); 
    var comments = jQuery.parseJSON(request.responseText).items; 

    if (debug){ 
     for (comment in comments){ 
     console.log(comment); 
     } 
    } 

    }else{ 
    handleRequestIssue(request); 
    } 

    return comments; 
} 


function manualTrigger(){ 
    var activities = getActivities("109716647623830091721"); 
} 

以下代码将它汇集在一起​​并检索活动和评论的具体职位:

$(document).ready(function() { 

    var renderMe = ""; 
    var activities = getActivities("109716647623830091721"); 

    console.log("activities retrieved: " + activities.length); 

    for (var i=0; i < activities.length; i++) { 
    console.log("trying to do something with an activity: " + i); 
    var activity = activities[i]; 

    renderMe += "<br/><div class=\"article\"><p>" + activity.title + "</p>"; 
    console.log(activity.id); 

    // get comments 
    var comments = getCommentsForActivity(activity.id); 
    for (var j=0; j<comments.length; j++){ 
     renderMe += "<br/><div class=\"comment\">" + comments[j].object.content + "</div>"; 
    } 
    renderMe += "</div>"; 
    } 
    console.log("I'm done"); 

    document.getElementById("ac").innerHTML = renderMe; 
}); 
+0

这是伟大的,因为在JavaScript中的同源策略我不确定代码可以工作,但无论如何感谢您的帮助 – user1961278

相关问题