Last active
August 29, 2015 14:04
-
-
Save junojisan/b4306b5293319153eea2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public PageReference getAccounts(){ | |
// RestAPIのURLを組立て | |
String restApiUrl = | |
oauthModel.instance_url + | |
'/services/data/v30.0/query/?q=' + | |
EncodingUtil.urlEncode('Select Id, Name From Account Limit 10', 'UTF-8'); | |
// Callout | |
Http http = new Http(); | |
HttpRequest req = new HttpRequest(); | |
// ※Headerにアクセストークンを設定する | |
req.setHeader('Authorization', 'Bearer ' + oauthModel.access_token); | |
req.setEndpoint(restApiUrl); | |
req.setMethod('GET'); | |
HttpResponse res = http.send(req); | |
// Statusの確認 | |
if(res.getStatusCode() == 401){ | |
// アクセストークンの有効期限が切れている場合、 | |
// リフレッシュトークンを使ってアクセストークンを取得 | |
// ※サンプルでは割愛 | |
// refreshToken(); | |
} | |
// レスポンスを解析し取引先のインスタンス生成とリストに追加 | |
JSONParser parser = JSON.createParser(res.getBody()); | |
accounts = new List<Account>(); | |
while(parser.nextToken() != null){ | |
if(parser.getCurrentToken() == JSONToken.START_ARRAY){ | |
while(parser.nextToken() != null){ | |
if(parser.getCurrentToken() == JSONToken.START_OBJECT){ | |
Account acc = (Account)parser.readValueAs(Account.class); | |
accounts.add(acc); | |
} | |
} | |
} | |
} | |
return null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment