0
我使用Google+ Badge API獲取關注者和+1。另外,我使用Google+ Signin API登錄按鈕。當我使用登錄按鈕登錄時,它要求獲得電子郵件許可。但是,當我要關注或+1 Google+頁面時,它會要求我登錄並且從不要求權限。如何使用Google+ Badge API索取權限?
<html>
<head>
<title>Demo: Getting an email address using the Google+ Sign-in button</title>
<style type="text/css">
.hide { display: none;}
.show { display: block;}
</style>
<script src="https://apis.google.com/js/plusone.js" type="text/javascript"></script>
<script type="text/javascript">
/*
* Triggered when the user accepts the the sign in, cancels, or closes the
* authorization dialog.
*/
function loginFinishedCallback(authResult) {
if (authResult) {
if (authResult['error'] == undefined){
gapi.auth.setToken(authResult); // Store the returned token.
toggleElement('signin-button'); // Hide the sign-in button after successfully signing in the user.
getEmail(); // Trigger request to get the email address.
} else {
console.log('An error occurred');
}
} else {
console.log('Empty authResult'); // Something went wrong
}
}
/*
* Initiates the request to the userinfo endpoint to get the user's email
* address. This function relies on the gapi.auth.setToken containing a valid
* OAuth access token.
*
* When the request completes, the getEmailCallback is triggered and passed
* the result of the request.
*/
function getEmail(){
// Load the oauth2 libraries to enable the userinfo methods.
gapi.client.load('oauth2', 'v2', function() {
var request = gapi.client.oauth2.userinfo.get();
request.execute(getEmailCallback);
});
// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
// https://developers.google.com/+/quickstart/javascript
gapi.client.load('plus','v1', function(){
var request = gapi.client.plus.people.list({
'userId': 'me',
'collection': 'visible'
});
request.execute(function(resp) {
console.log('Num people visible:' + resp.totalItems);
});
});
// This sample assumes a client object has been created.
// To learn more about creating a client, check out the starter:
// https://developers.google.com/+/quickstart/javascript
gapi.client.load('plus','v1', function(){
var request = gapi.client.plus.people.get({
'userId': 'me'
});
request.execute(function(resp) {
console.log('Retrieved profile for:' + resp.displayName);
console.log(resp);
});
});
}
function getEmailCallback(obj){
var el = document.getElementById('email');
var email = '';
if (obj['email']) {
email = 'Email: ' + obj['email'];
}
//console.log(obj); // Uncomment to inspect the full object.
el.innerHTML = email;
toggleElement('email');
}
function toggleElement(id) {
var el = document.getElementById(id);
if (el.getAttribute('class') == 'hide') {
el.setAttribute('class', 'show');
} else {
el.setAttribute('class', 'hide');
}
}
</script>
</head>
<body>
<div id="signin-button" class="show">
<div class="g-signin" data-callback="loginFinishedCallback"
data-clientid="MY_CLIENT_ID"
data-scope="https://www.googleapis.com/auth/plus.login https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email"
data-height="short"
data-cookiepolicy="single_host_origin"
>
</div>
</div>
<!-- Place this tag where you want the badge to render. -->
<div class="g-plus" data-width="210" data-href="//plus.google.com/MY_PAGE_ID" data-rel="publisher" data-theme="dark"></div>
<div id="email" class="hide"></div>
</body>
</html>
有沒有解決,我可以登錄時使用跟蹤和+1按鈕要求授權用戶的方式?
此外,如果用戶設置了他/她的個人資料欄位(如生日)隱私「只有我」,然後我可以請求這些領域?如果是,如何?
1 - 是的,有一個Google+徽章API。在這裏你去 - https://developers.google.com/+/web/badge/ 2 - 我認爲üdin't得到我的問題的兄弟!我想在用戶點擊「關注」或「+1」時獲得用戶的權限。現在,我只能通過「登錄」按鈕實現它 – spyder
你說得對,我們有渲染徽章一個Javascript API:https://developers.google.com/+/web/badge/#jsapi 無的這些徽章有回調,當用戶點擊它們時可以觸發行動。 +1按鈕確實有一個回調,可能會導致你的函數被調用:https://developers.google.com/+/web/+1button/ 我反對這樣做,但你可以設置自己的回調在父元素上並讓它觸發登錄流程。你會有效地黑客自定義渲染功能:https://developers.google.com/+/web/signin/#customizing_the_sign-in_button – ade