2013-03-01 11 views
5

Authorize requests using OAuth 2.0: ON के साथ g + दस्तावेज़ से example को आजमाने में असमर्थ। परिणामस्वरूप Unauthorized मिला। यहां आउटपुट है:Google+

Request 

POST https://www.googleapis.com/plus/v1/people/me/moments/vault?debug=true&key={YOUR_API_KEY} 

Content-Type: application/json 
Authorization: Bearer *my_token* 
X-JavaScript-User-Agent: Google APIs Explorer 

{ 
"target": { 
    "url": "https://developers.google.com/+/web/snippet/examples/thing" 
}, 
"type": "http://schemas.google.com/AddActivity" 
} 

Response 


401 Unauthorized 

cache-control: private, max-age=0 
content-encoding: gzip 
content-length: 93 
content-type: application/json; charset=UTF-8 
date: Fri, 01 Mar 2013 18:56:34 GMT 
expires: Fri, 01 Mar 2013 18:56:34 GMT 
server: GSE 
www-authenticate: AuthSub realm="https://www.google.com/accounts/AuthSubRequest" allowed-scopes="https://www.googleapis.com/auth/plus.login,https://www.google.com/accounts/OAuthLogin" 

{ 
"error": { 
    "errors": [ 
    { 
    "message": "Unauthorized" 
    } 
    ], 
    "code": 401, 
    "message": "Unauthorized" 
} 
} 

Google एपीआई एक्सप्लोरर अनुमतियों को निरस्त करने और फिर से प्रमाणीकृत करने का प्रयास किया। कुछ नहीं बदला। क्या मैं कुछ गलत कर रहा हूं या जी + एपिस अभी तक उत्पादन के उपयोग के लिए तैयार नहीं हैं?

उत्तर

3

ऐसा लगता है कि एपीआई एक्सप्लोरर वर्तमान में Google को लेखन ऐप गतिविधि के साथ काम नहीं करेगा क्योंकि यह OAUTH2 प्रवाह के अनुरोध अनुरोध फ़ील्ड को पास नहीं कर रहा है। आप अभी भी चीजों को मैन्युअल रूप से कर सकते हैं जैसा कि मैं नीचे वर्णित करूंगा।

सबसे पहले, यह सुनिश्चित करें कि आप अनुप्रयोग गतिविधि प्रकार आप डालने कर रहे हैं के लिए निर्धारित requestvisibleactions साथ साइन-इन बटन प्रदान कर रहे हैं:

वहाँ दो चीजें आप क्या करने की जरूरत है। निम्न उदाहरण दिखाता है कि आप ऐड गतिविधि के साथ में साइन-प्रस्तुत करना होगा:

<div id="gConnect"> 
    <button class="g-signin" 
     data-scope="https://www.googleapis.com/auth/plus.login" 
     data-requestvisibleactions="http://schemas.google.com/AddActivity" 
     data-clientId="YOUR_CLIENT_ID" 
     data-callback="onSignInCallback" 
     data-theme="dark" 
     data-cookiepolicy="single_host_origin"> 
    </button> 
</div> 

इसके बाद, आप का निर्माण और एप्लिकेशन गतिविधि लिखने के लिए की आवश्यकता होगी।

:

http://wheresgus.com/appactivitiesdemo

आप प्रलेखन यहां से लगभग गतिविधि प्रकार के सभी सीख सकते हैं:

var payload = { 
    "target": { 
     "id" : "replacewithuniqueidforaddtarget", 
     "image" : "http:\/\/www.google.com\/s2\/static\/images\/GoogleyEyes.png", 
     "type" : "http:\/\/schema.org\/CreativeWork", 
     "description" : "The description for the activity", 
     "name":"An example of AddActivity" 
    }, 
    "type":"http:\/\/schemas.google.com\/AddActivity", 
    "startDate": "2012-10-31T23:59:59.999Z" 
    }; 
    var args = { 
    'path': '/plus/v1/people/me/moments/vault', 
    'method': 'POST', 
    'body': JSON.stringify(payload), 
    'callback': function(response) { 
     console.log(response); 
    } 
    }; 

    gapi.client.request(args); 

आप यहाँ एक लाइव डेमो देख सकते हैं: निम्न उदाहरण इस का उपयोग कर जावास्क्रिप्ट चलता https://developers.google.com/+/api/moment-types

अद्यतन

नोट लाइव डेमो निम्न कोड के साथ अद्यतन किया गया है और आप सीधे फोन कर नहीं किया जाना चाहिए gapi.client.request:

writeListenActivity: function(url){ 
    var payload = { 
    "type": "http://schemas.google.com/ListenActivity", 
    } 

    if (url != undefined){ 
    payload.target = { 'url' : url }; 
    }else{ 
    payload.target = { 
     "type": "http:\/\/schema.org\/MusicRecording", 
     "id": "uniqueidformusictarget", 
     "description": "A song about missing one's family members fighting in the American Civil War", 
     "image": "https:\/\/developers.google.com\/+\/plugins\/snippet\/examples\/song.png", 
     "name": "When Johnny Comes Marching Home" 
    }; 
    } 
    this.writeAppActivity(payload); 
}, 
writeAddActivity: function(url){ 
    var payload = { 
    "type":"http:\/\/schemas.google.com\/AddActivity", 
    "startDate": "2012-10-31T23:59:59.999Z" 
    }; 
    if (url != undefined){ 
    payload.target = { 
     'url' : 'https://developers.google.com/+/plugins/snippet/examples/thing' 
    }; 
    }else{ 
    payload.target = { 
     "id" : "replacewithuniqueidforaddtarget", 
     "image" : "http:\/\/www.google.com\/s2\/static\/images\/GoogleyEyes.png", 
     "type" : "http:\/\/schema.org\/CreativeWork", 
     "description" : "The description for the activity", 
     "name":"An example of AddActivity" 
    }; 
    } 
    this.writeAppActivity(payload); 
}, 
writeAppActivity: function(payload){ 

    gapi.client.plus.moments.insert(
     { 'userId' : 'me', 
     'collection' : 'vault', 
     'resource' : payload 
     }).execute(function(result){ 
      console.log(result); 
     }); 
} 
विशेष ध्यान दें

gapi.client.plus.moments.insert कोड है जो gapi.client.request कॉल को प्रतिस्थापित करता है।

+2

ग्रेट, 'requestvisibleactions' जैसा दिखता है समस्या की जड़ है। और [यह पोस्ट] (https://plus.google.com/118276561380249048216/posts/2kMX9Dzaf8V) उचित [diff] (https://code.google.com/p/google-api-php-client/source) तक ले जाता है सर्वर-साइड ओथ प्रमाणीकरण के लिए /diff?spec=svn533&r=533&format=side&path=/trunk/src/auth/Google_OAuth2.php)। जानकारी के लिए धन्यवाद! – mente

+0

@class "replacewithuniqueidforaddtarget" से आपका क्या मतलब है क्या आप यहां एक यादृच्छिक आईडी टाइप करते हैं या कुछ? – Supertecnoboff

+0

लक्ष्य आईडी के लिए, जैसा कि आपने सुझाव दिया है, यह सिर्फ एक यादृच्छिक स्ट्रिंग है। – class

संबंधित मुद्दे