好,如果別人需要這個,這是我做的:
我有三個文件,視圖,啓動控制器,並結束控制。
上來看,我有一個這樣的鏈接:
<g:link action="registerOnLinkedIn" controller="linkedinProfile" >connect </g:link>
其中我有這樣的方法:
String apiKey =:myKey"
String apiSecret="mySecret"
String callBackUrl="http://localhost:8080/myApp/secure/mySub/success"
def registerOnLinkedIn = {
Token linkedInAccessToken=null;
OAuthService service=new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.callback(callBackUrl)
.build();
Token requestToken = service.getRequestToken();
String authUrl = service.getAuthorizationUrl(requestToken);
session['REQUEST_TOKEN'] = requestToken
redirect(url: authUrl)
}
def success ={
String v = params.oauth_verifier
String r= session['REQUEST_TOKEN']
linkedInXmlService.getXmlStream(v,session['REQUEST_TOKEN'])
}
當鏈路上的用戶點擊,它們被髮送到該方法中,這會創建一個重定向網址。重定向url是linkedIn的授權頁面,用戶可以在其中接受該應用程序。一旦被接受,他們被重定向到成功方法,該方法被重定向到服務。
該服務獲取驗證者和令牌並向linkedin API發送請求。其大部分是:
def apiUrl = "http://api.linkedin.com/v1/people/~:(" +
"id," +
"picture-url," +
"site-standard-profile-request," +
"first-name," +
"date-of-birth," +
"last-name," +
"industry," +
"location," +
"educations," +
"positions:(id,title,summary,start-date,end-date,is-current,company)," +
"skills:(id,skill:(name),proficiency:(level),years:(name))," +
"connections:(id,industry,first-name,last-name,site-standard-profile-request,headline,location,positions,educations,date-of-birth,picture-url,skills:(id,skill:(name),proficiency:(level),years:(name)))" +
")"
public void getXmlStream(String ver, rt)
{
String accessTokenKey=""
String accessTokenSecret=""
String xmlString =""
OAuthService service=new ServiceBuilder()
.provider(LinkedInApi.class)
.apiKey(apiKey)
.apiSecret(apiSecret)
.build();
Verifier v = new Verifier(ver);
Token accessToken = service.getAccessToken(rt, v);
accessTokenSecret = accessToken.secret
accessTokenKey = accessToken.token
OAuthRequest request = new OAuthRequest(Verb.GET, apiUrl);
service.signRequest(accessToken, request); // the access token from step 4
Response response = request.send();
xmlString=response.getBody();
log.debug (xmlString)
processData(xmlString, accessTokenKey, accessTokenSecret)
}
具體而言,我不認爲我有successUri和failureUri屬性設置正確...我是新來的,但不管它通過設置回調屬性解決了您遇到的問題。 – JSager